ITPub博客

首页 > 数据库 > 数据库开发技术 > SpringCloud Feign遇到的问题

SpringCloud Feign遇到的问题

原创 数据库开发技术 作者:HelloTech技术派 时间:2017-12-07 18:00:00 0 删除 编辑

问题一:由于马虎,缺少配置,报错如下:

  1. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.gemantic.springcloud.service.AdditionClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method add not annotated with HTTP method type (ex. GET, POST)

  2. Caused by: java.lang.IllegalStateException: Method add not annotated with HTTP method type (ex. GET, POST)

解决方案,在Feign接口上没有配置请求式,在接口上增加请求方式 @GetMapping(value="/add") 或 @RequestMapping(value="/add"),完整代码如下:

  1. @FeignClient("springcloud-addition-service")

  2. public interface AdditionClient {

  3.    /**

  4.     * 加法接口

  5.     * @param a

  6.     * @param b

  7.     * @return

  8.     */

  9. //    @RequestMapping(value = "/add")

  10.    @GetMapping(value = "/add")

  11.    ResponseEntity<ResultData> add(@RequestParam("a") @NotNull Integer a, @RequestParam("b") @NotNull Integer b);

  12. }

注: value="/add" 与真正服务一致。

问题二:

  1. Caused by: java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0

暴漏Restful服务的方法如下:

  1. @Slf4j

  2. @RestController

  3. @Api(description = "加法接口")

  4. public class AdditionController {

  5.    @Autowired

  6.    private Addition addition;

  7.    @GetMapping(value = "/add")

  8.    @ApiOperation(value = "a + b = ?")

  9.    public ResponseEntity<ResultData> add(@RequestParam @NotNull Integer a, @RequestParam @NotNull Integer b) {

  10.        if (log.isInfoEnabled()) {

  11.            log.info("add {} + {}", a, b);

  12.        }

  13.        int result = addition.add(a, b);

  14.        ResultData resultData = ResultData.builder().message(Message.builder().code(0).message(Message.SUCCESS_MESSAGE).build()).build();

  15.        resultData.setData(result);

  16.        return ResponseEntity.ok(resultData);

  17.    }

  18. }

Feign的方式的接口如下:

  1. @GetMapping(value = "/add")

  2. ResponseEntity<ResultData> add(@RequestParam @NotNull Integer a, @RequestParam @NotNull Integer b);

服务启动时就会报错误:

  1. Caused by: java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0

解决方案:在 @RequestParam 增加方法名

  1. @GetMapping(value = "/add")

  2. ResponseEntity<ResultData> add(@RequestParam("a") @NotNull Integer a, @RequestParam("b") @NotNull Integer b);

注: @PathVariable 与 @RequestParam类似的。

问题三:

  1. Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity com.gemantic.springcloud.service.AdditionClient.add(java.lang.Integer,java.lang.Integer)

解决方案:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上 @RequestParam或 @PathVariable注解修饰,Controller里面可以不加该注解修饰。

  1. @GetMapping(value = "/add")

  2. ResponseEntity<ResultData> add(@RequestParam("a") @NotNull Integer a, @RequestParam("b") @NotNull Integer b);


原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73722