欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-03 19:46:10
springcloud feign传对象参数调用失败 不支持get请求方式 使用apache httpclient替换feign原生httpclient @requestbody接收...

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

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

上一篇:

下一篇: