亲宝软件园·资讯

展开

Feign参数调用失败 解决SpringCloud Feign传对象参数调用失败的问题

itdragons 人气:1
想了解解决SpringCloud Feign传对象参数调用失败的问题的相关内容吗,itdragons在本文为您仔细讲解Feign参数调用失败的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringCloud,Feign参数调用失败,Feign传对象,下面大家一起来学习吧。

SpringCloud Feign传对象参数调用失败

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("调用test\n");
       return currentUser;
    }
}

SpringCloud中Feign异常无法传递的问题

因为 cloud内部抛出异常不进行处理,Feign获取spring默认包装异常结果如下:

{
"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
@RestControllerAdvice
public 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;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论