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

 更新时间:2021年06月23日 16:12:58   作者:itdragons  
这篇文章主要介绍了解决SpringCloud Feign传对象参数调用失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringCloud Feign传对象参数调用失败

  • 不支持GET请求方式
  • 使用Apache HttpClient替换Feign原生httpclient
  • @RequestBody接收json参数

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;
    }
}

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

相关文章

  • 浅析Java中的动态代理

    浅析Java中的动态代理

    动态代理指代理类和目标类的关系在程序运行的时候确定的,客户通过代理类来调用目标对象的方法。本文将通过案例详细讲解一下Java动态代理的原理及实现,需要的可以参考一下
    2022-09-09
  • springboot访问静态资源遇到的坑及解决

    springboot访问静态资源遇到的坑及解决

    这篇文章主要介绍了springboot访问静态资源遇到的坑及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 使用Spirng Boot Admin监控Spring Cloud应用项目

    使用Spirng Boot Admin监控Spring Cloud应用项目

    这篇文章主要介绍了使用Spirng Boot Admin监控Spring Cloud应用项目,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • SpringBoot如何配置Controller实现Web请求处理

    SpringBoot如何配置Controller实现Web请求处理

    这篇文章主要介绍了SpringBoot如何配置Controller实现Web请求处理,文中通过图解示例介绍的很详细,具有有一定的参考价值,需要的小伙伴可以参考一下
    2023-05-05
  • 新手了解java 多线程基础知识

    新手了解java 多线程基础知识

    这篇文章主要介绍了java 基础教程之多线程详解及简单实例的相关资料,线程的基本属性、如何创建线程、线程的状态切换以及线程通信,需要的朋友可以参考下
    2021-07-07
  • Classloader隔离技术在业务监控中的应用详解

    Classloader隔离技术在业务监控中的应用详解

    这篇文章主要为大家介绍了Classloader隔离技术在业务监控中的应用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • SpringBoot缓存Ehcache的使用详解

    SpringBoot缓存Ehcache的使用详解

    EhCache、Redis比较常用,使用Redis的时候需要先安装Redis服务器,本文给大家介绍SpringBoot缓存Ehcache的使用详解,感兴趣的朋友跟随小编一起看看吧
    2022-03-03
  • Java 动态代理你真的懂了吗(动态和代理)

    Java 动态代理你真的懂了吗(动态和代理)

    动态代理分两部分,动态和代理,今天通过本文给大家普及代码模式及动态代理的概念及示例代码,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • Java将时间戳转换为Date对象的方法小结

    Java将时间戳转换为Date对象的方法小结

    在 Java 编程中,处理日期和时间是一个常见需求,特别是在处理网络通信或者数据库操作时,本文主要为大家整理了Java中将时间戳转换为Date对象的方法,希望对大家有所帮助
    2024-12-12
  • SpringBoot实现拦截器、过滤器、监听器过程解析

    SpringBoot实现拦截器、过滤器、监听器过程解析

    这篇文章主要介绍了SpringBoot实现拦截器、过滤器、监听器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12

最新评论