本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
解决SpringCloud Feign传对象参数调用失败的问题
程序猿小军 · 292浏览 · 发布于2021-06-23 +关注

这篇文章主要介绍了解决SpringCloud Feign传对象参数调用失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringCloud Feign传对象参数调用失败

  • 不支持GET请求方式

  • 使用Apache HttpClient替换Feign原生httpclient

  • @RequestBody接收json参数

bootstrap-local.yml

1

2

3

feign:

  httpclient:

    enabled: true

pom.xml

1

2

3

4

5

6

7

8

9

10

11

<!-- 使用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:

1

2

3

4

5

@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:


1

2

3

4

5

6

7

8

9

10

@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"
}

自定义的异常处理下返回状态

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

    }

}


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 962浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
我是来自差了一点掉完头发的程序猿,小军,希望在这里可以向各位大佬们学习。
分类专栏
小鸟云服务器
扫码进入手机网页