记一次Feign中实现传实体Bean的问题
目录
- Feign如何传实体Bean
- Feign注入Bean为null,启动报错
- swagger冲突版本
- 解决方案
- feignService案例
- controller注入
Feign如何传实体Bean
需要加一个@RequestBody序列化对象
/** * 新增 */ @PostMapping("saveOrder") @ResponseBody public void saveOrder(@RequestBody OrderBean orderBean) { userService.saveOrder(orderBean); }
接口接收请求也要加一个@RequestBody注解
/** * 新增 * @param orderBean */ @PostMapping("saveOrder") public void saveOrder(@RequestBody OrderBean orderBean);
Feign注入Bean为null,启动报错
主要是feign版本和swagger版本冲突,本项目spring cloud 版本为 Finchley.M8
swagger冲突版本
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
解决方案
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency>
feign Service案例
@FeignClient(name = "service-weixin",fallback = OAuthServiceFallBack.class, configuration = FeignConfig.class) public interface OAuthService { @RequestMapping(value = "/oauth/getOauth2AuthorizationUrl", method = RequestMethod.GET) ResultBean<String> getOauth2AuthorizationUrl(@RequestParam("url")String url); }
controller注入
@RestController @RequestMapping("/ticket") public class TicketController { @Autowired private TicketService ticketService; @Autowired private OAuthService oAuthService; ...
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)