好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

记一次Feign中实现传实体Bean的问题

Feign如何传实体Bean

需要加一个@RequestBody序列化对象

?

1

2

3

4

5

6

7

8

    /**

      * 新增

      */

    @PostMapping ( "saveOrder" )

    @ResponseBody

    public void saveOrder( @RequestBody OrderBean orderBean) {

        userService.saveOrder(orderBean);

    }

接口接收请求也要加一个@RequestBody注解 

?

1

2

3

4

5

6

    /**

      * 新增

      * @param orderBean

      */

    @PostMapping ( "saveOrder" )

    public void saveOrder( @RequestBody OrderBean orderBean);

Feign注入Bean为null,启动报错

主要是feign版本和swagger版本冲突,本项目spring cloud 版本为 Finchley.M8

swagger冲突版本

?

1

2

3

4

5

6

7

8

9

10

11

        <!-- 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 >

解决方案

?

1

2

3

4

5

6

7

8

9

10

11

        <!-- 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案例

?

1

2

3

4

5

@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注入

?

1

2

3

4

5

6

7

8

@RestController

@RequestMapping ( "/ticket" )

public class TicketController {

    @Autowired

    private TicketService ticketService;

    @Autowired

    private OAuthService oAuthService;

    ...

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

原文链接:https://blog.csdn.net/SpringCYB/article/details/89208707

查看更多关于记一次Feign中实现传实体Bean的问题的详细内容...

  阅读:16次