解決SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗的問題
SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗
- 不支持GET請(qǐng)求方式
- 使用Apache HttpClient替換Feign原生httpclient
- @RequestBody接收json參數(shù)
bootstrap-local.yml
feign: httpclient: enabled: true
pom.xml
<!-- 使用Apache HttpClient替換Feign原生httpclient --> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-httpclient</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency>
feignClient:
@FeignClient(name = "hd-ucenter-server", fallback = SysTestServerFallbackImpl.class) public interface SysTestServer { @RequestMapping(value = "/test/test", method = RequestMethod.POST, consumes = "application/json") Object test(CurrentUser currentUser); }
RestController:
@RestController @PostMapping("/test") public class TestController { @RequestMapping(value = "/test") public Object test(@RequestBody CurrentUser currentUser) { System.out.printf("調(diào)用test\n"); return currentUser; } }
SpringCloud中Feign異常無法傳遞的問題
因?yàn)?cloud內(nèi)部拋出異常不進(jìn)行處理,F(xiàn)eign獲取spring默認(rèn)包裝異常結(jié)果如下:
{
"timestamp": "2017-12-27 15:01:53",
"status": 500,
"error": "Internal Server Error",
"exception": "com.keruyun.loyalty.commons.master.exception.BusinessException",
"message": "Request processing failed; nested exception is {\"code\":1000, \"message\":\"test Exception\"}",
"path": "/coupon/cloud/commercial/8469"
}
自定義的異常處理下返回狀態(tài)
@Slf4j @RestControllerAdvice public class GlobalExceptionHandlerResolver { //內(nèi)部服務(wù)異常處理 @ExceptionHandler(InternalApiException.class) public ResultResp<?> handleGlobalException(HttpServletResponse response, InternalApiException internalApiException) { ResultResp<?> resultResp = internalApiException.getResultResp(); log.error(internalApiException.getMessage(), internalApiException); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());//返回500異常 response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString()); return resultResp; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?整合Security權(quán)限控制的初步配置
這篇文章主要為大家介紹了SpringBoot?整合Security權(quán)限控制的初步配置實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11sqlite數(shù)據(jù)庫的介紹與java操作sqlite的實(shí)例講解
今天小編就為大家分享一篇關(guān)于sqlite數(shù)據(jù)庫的介紹與java操作sqlite的實(shí)例講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02java+jdbc+mysql+socket搭建局域網(wǎng)聊天室
這篇文章主要為大家詳細(xì)介紹了java+jdbc+mysql+socket搭建局域網(wǎng)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Java?spring?boot發(fā)送郵箱實(shí)現(xiàn)過程記錄
我們?cè)?站上注冊(cè)賬號(hào)的時(shí)候?般需要獲取驗(yàn)證碼,?這個(gè)驗(yàn)證碼?般發(fā)送在你的?機(jī)號(hào)上還有的是發(fā)送在你的郵箱中,這篇文章主要給大家介紹了關(guān)于Java?spring?boot發(fā)送郵箱實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01JAVA中讀取文件(二進(jìn)制,字符)內(nèi)容的幾種方法總結(jié)
本篇文章主要介紹了JAVA中讀取文件(二進(jìn)制,字符)內(nèi)容的方法總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02