问题一:
在前面的示例中,我们讲过
- @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
- @GetMapping("/user/{id}")
- Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)
问题二:
在前面的示例中,我们暴漏Restful服务的方式如下:
- @GetMapping("/template/{id}")
- public User findById(@PathVariable Long id) {
- return client.findById(id);
- }
- @PathVariable Long id
- @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
- User findById(@PathVariable Long id);
- Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
- @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
- User findById(@PathVariable("id") Long id);
再启动,发现一切都ok了。
问题三:多参数问题
- @RequestMapping(value="/user/name", method=RequestMethod.GET)
- User findByUsername(final String userName, final String address);
- Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.chhliu.springboot.restful.vo.User com.chhliu.springboot.restful.feignclient.UserFeignClient.findByUsername(java.lang.String,java.lang.String)
异常原因:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰。
上面问题的解决方案如下:
- @RequestMapping(value="/user/name", method=RequestMethod.GET)
- User findByUsername(@RequestParam("userName") final String userName, @RequestParam("address") final String address);
问题四:Request method 'POST' not supported
错误代码示例:
- @RequestMapping(value="/user/name", method=RequestMethod.GET)
- User findByUsername(final String userName, @RequestParam("address") final String address);
注意:上面的userName参数没有用@RequestParam注解修饰,然后发送请求,会发现被调用的服务一直报Request method 'POST' not supported,我们明明使用的是GET方法,为什么被调用服务认为是POST方法了,原因是当userName没有被@RequestParam注解修饰时,会自动被当做request body来处理。只要有body,就会被feign认为是post请求,所以整个服务是被当作带有request parameter和body的post请求发送出去的。