您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“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; } }
因?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; } }
到此,相信大家對(duì)“SpringCloud Feign傳對(duì)象參數(shù)調(diào)用失敗怎么解決”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。