SpringCloud Feign传对象参数调用失败怎么解决

发布时间:2021-06-24 09:12:40 作者:chen
来源:亿速云 阅读:476

本篇内容主要讲解“SpringCloud Feign传对象参数调用失败怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringCloud 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;
    }
}

到此,相信大家对“SpringCloud Feign传对象参数调用失败怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何使用SpringCloud Feign 调用服务
  2. SpringCloud中使用Feign时会遇到哪些问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springcloud feign

上一篇:vue-router中hash模式与history模式有哪些区别

下一篇:怎么用Python的openpyxl批量处理数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》