解决SpringCloud|解决SpringCloud Feign传对象参数调用失败的问题
SpringCloud Feign传对象参数调用失败
- 不支持GET请求方式
- 使用Apache HttpClient替换Feign原生httpclient
- @RequestBody接收json参数
feign:httpclient:enabled: true
pom.xml
com.netflix.feign feign-httpclient8.18.0 org.apache.httpcomponents httpclient4.5.3
feignClient:
@FeignClient(name = "hd-ucenter-server", fallback = SysTestServerFallbackImpl.class)public interface SysTestServer { @RequestMapping(value = "https://www.it610.com/test/test", method = RequestMethod.POST, consumes = "application/json")Object test(CurrentUser currentUser); }
RestController:
@RestController@PostMapping("/test")public class TestController { @RequestMapping(value = "https://www.it610.com/test")public Object test(@RequestBody CurrentUser currentUser) {System.out.printf("调用test\n"); return currentUser; }}
SpringCloud中Feign异常无法传递的问题 因为 cloud内部抛出异常不进行处理,Feign获取spring默认包装异常结果如下:
【解决SpringCloud|解决SpringCloud Feign传对象参数调用失败的问题】{自定义的异常处理下返回状态
"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"
}
@Slf4j@RestControllerAdvicepublic class GlobalExceptionHandlerResolver { //内部服务异常处理@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; }}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- Spark|Spark 数据倾斜及其解决方案
- 解决SyntaxError:|解决SyntaxError: invalid syntax
- Spectrum|Spectrum 区块偶尔停止同步问题排查与解决笔记
- 一劳永逸地解决词汇量不够的问题
- Hexo代码块前后空白行问题