av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring Boot FeignClient 如何捕獲業務異常信息

瀏覽:29日期:2023-07-05 10:19:01
Spring Boot FeignClient 捕獲業務異常信息

因項目重構采用spring cloud,feign不可避免。目前spring cloud在國內還不是很成熟,所以踩坑是免不了的。最近處理全局異常的問題,搜了個遍也沒找到合適的解決方案

1.全局異常處理

import com.bossien.common.comm.entity.ResponseDto;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;@ControllerAdvicepublic class GlobalExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * @Author: lixg * @Description: 系統異常捕獲處理 */ @ResponseBody @ExceptionHandler(value = Exception.class) public ResponseDto errorExceptionHandler(Exception ex) {//APIResponse是項目中對外統一的出口封裝,可以根據自身項目的需求做相應更改logger.error('捕獲到 Exception 異常', ex);//異常日志入庫return new ResponseDto(ResponseDto.RESPONSE_FAIL, '系統繁忙,請稍后再試'); } /** * @Author: lixg * @Description: 自定義異常捕獲處理 */ @ResponseBody @ExceptionHandler(value = BusinessException.class)//BusinessException是自定義的一個異常 public ResponseDto businessExceptionHandler(BusinessException ex) {logger.error('捕獲到 BusinessException 異常: code=' + ex.getCode() + ' , errorMessage=' + ex.getErrorMessage());return new ResponseDto(ex.getCode(), ex.getErrorMessage()); }}2.請求參數解析handler

import com.alibaba.fastjson.JSONObject;import com.ocean.common.comm.entity.ResponseDto;import com.ocean.common.core.exception.BusinessException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/*** * @author lixg * * feign請求響應對象處理 */public class ResponseHandler { private final static Logger logger = LoggerFactory.getLogger(ResponseHandler.class); /** * 解析請求響應對象 * @param responseDto * @param clazz * @return * @throws BusinessException */ public static Object getResponseData(ResponseDto responseDto, Class clazz) throws BusinessException {if(EmptyUtil.isEmpty(responseDto)){ throw new BusinessException(BusinessException.OBJECT_IS_NULL,'請求響應為空!');}if(ResponseDto.RESPONSE_SUCCESS.equals(responseDto.getCode())){ try {String json = JSONObject.toJSONString(responseDto.getData());return JSONObject.parseObject(json, clazz); }catch (Exception e){logger.error('響應對象轉換異常:'+clazz.getName(),e);throw new BusinessException(BusinessException.OBJECT_IS_NULL,'響應對象轉換失敗!'); }}else{ throw new BusinessException(responseDto.getCode(),responseDto.getMessage());} }}3.業務feign接口

package com.bossien.usercenter.user.feign;import com.bossien.common.comm.entity.ResponseDto;import com.bossien.common.comm.util.PageModel;import com.bossien.common.comm.constant.SearchEntity;import com.bossien.common.core.exception.BusinessException;import com.bossien.usercenter.user.entity.User;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.stereotype.Repository;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import java.util.List;import java.util.Map;@FeignClient(value='bossien-usercenter-service',path = '/userFeign')@Repositorypublic interface UserFeign { @RequestMapping(value = 'getUserInfo',method = RequestMethod.GET) User getUserInfo(@RequestParam('userId') Long userId); @RequestMapping(value = 'getUserInfoByTicket',method = RequestMethod.GET) ResponseDto getUserInfoByTicket(@RequestParam('ticket') String ticket) throws BusinessException; }總結:

@controllerAdvice或者HandlerExceptionResolver是不能直接捕獲到FeignException,所以需要在Feign層面拿到具體異常重新封裝。最后總算把cloud service內部的異常安全(一樣的錯誤碼、一樣的錯誤信息)送給了client!!

Feign調用異常處理

consumer服務調用Producer服務接口時,提示一下異常

no suitable HttpMessageConverter found for request type

feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [com.xxx.pojo.Xxx] and content type [application/x-www-form-urlencoded] at org.springframework.cloud.openfeign.support.SpringEncoder.encode(SpringEncoder.java:143) ~[spring-cloud-openfeign-core-2.1.0.RELEASE.jar:2.1.0.RELEASE] at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:372) ~[feign-core-10.1.0.jar:na] at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:224) ~[feign-core-10.1.0.jar:na] at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:74) ~[feign-core-10.1.0.jar:na] at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:106) ~[feign-hystrix-10.1.0.jar:na] at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298) ~[hystrix-core-1.5.18.jar:1.5.18] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.2.0.jar:1.2.0] at rx.Observable.unsafeSubscribe(Observable.java:10151) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.2.0.jar:1.2.0] at rx.Observable.unsafeSubscribe(Observable.java:10151) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.2.0.jar:1.2.0] at rx.Observable.unsafeSubscribe(Observable.java:10151) ~[rxjava-1.2.0.jar:1.2.0] at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) ~[rxjava-1.2.0.jar:1.2.0] at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) ~[hystrix-core-1.5.18.jar:1.5.18] at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) ~[rxjava-1.2.0.jar:1.2.0] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_221] at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_221] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_221] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_221] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]異常原因

如字面意思:

at org.springframework.cloud.openfeign.support.SpringEncoder.encode

缺少HttpMessageConverter 的編碼器

解決方法

缺少那就加進去

將SpringFormEncoder加入到容器中

import feign.codec.Encoder;import feign.form.spring.SpringFormEncoder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.context.annotation.Scope;/** * @author jianming * @create 2021-02-06-15:42 */@Configurationpublic class FeignSupportConfig { @Bean @Primary @Scope('prototype') public Encoder multipartFormEncoder() {return new SpringFormEncoder(); }}

問題處理完成

Consumer的Feign使用

處理需要上述的編碼器,還需在接口中指定ContentType

@Service@FeignClient(value = 'XXX-XXX')public interface LoginService { /** * 指定contentType */ @PostMapping(value = '/register', consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public MsgUtils create(User user);}

Producer正常編寫即可!以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 美女人人操 | 国产日批 | 日韩精品一区二区三区在线观看 | 精品国产高清一区二区三区 | 国产精品成人国产乱 | 精品久久国产 | 亚洲精品一区中文字幕乱码 | 日日干夜夜干 | 中文字幕 在线观看 | 亚洲一区二区三区四区五区中文 | 亚洲国产精品一区二区第一页 | 欧美日韩在线播放 | 欧美一级久久 | 久精品久久 | 久久大陆 | 看真人视频一级毛片 | 国精产品一品二品国精在线观看 | 欧美激情网站 | 91中文 | 国产成人亚洲精品 | 91精品国产欧美一区二区成人 | 成人免费视屏 | 久久久av | 国产亚洲精品久久久优势 | 亚洲精品一区中文字幕乱码 | 中文字幕一区在线 | 国外成人在线视频网站 | 午夜男人免费视频 | 一级黄色片网址 | 欧美黄色片| 国产精品久久在线观看 | 亚洲精品国产成人 | 国产一区二区激情视频 | 在线欧美一区 | 日韩一区二区在线视频 | 伊人一区 | 午夜小电影 | 国产高清视频在线观看播放 | 国产午夜久久 | 草草视频在线免费观看 | 亚洲综合色视频在线观看 |