springcloud feign接口传参详解

一.环境

springboot 2.2.1.RELEASE

springcloud Hoxton.RELEASE

compile 'io.github.openfeign:feign-httpclient'

compile 'org.springframework.cloud:spring-cloud-starter-openfeign'

二.各情况参数传递总结:

1.get传递普通参数,@GetMapping需要添加@RequestParam("msg")注解
2.不要用get传递封装类参数,如需传递封装类参数,用postmapping
3.post传递普通参数加@RequestBody或者@RequestParam都可以,但是不能什么都不加
4.post传递封装类参数,feign接口需要添加@RequestBody
5.post传递list参数:
    5.1.普通list,需要用数组接收,添加@RequestParam("lists")
    5.2.封装类list,需要添加@RequestBody

源码:

SdkTest.java
 

@Validated
public interface SdkTest {
    
    @GetMapping("test1")
    public String test(String msg);
    
    @GetMapping("test2")
    public Response test2(@RequestParam("msg") String msg);
    
    @PostMapping("test3")
    public Response test3(@RequestParam("msg") String msg);
    
    @PostMapping("test4")
    public Response test4(@RequestBody String msg);
    
    @PostMapping("test41")
    public Response test41(String msg);
    
    @PostMapping("test5")
    public Response> test5(@RequestParam("lists") String[] lists);
    
    @Data
    public static class User {
        
        private Long id;
        
        private Long appId;
        
        private String name;
        
        private String detail;
        
        private Integer age;
        
    }
    /** getmapping 接受封装对象也要用@RequestBody 否则feign接口获取到参数为空 
     *  
但是!get+body的话,原服务调用需要get传json才能获取到值,不方便传参,      *  
所以,获取对象传参这种方式,统一采用postmapping       * @see SdkTest#test8(User) */     @GetMapping("test6")     public Response test6(@RequestBody User user);          /** 如果没加@RequestBody 则feign接口过来获取参数为空 */     @GetMapping("test7")     public Response test7(User user);          @PostMapping("test8")     public Response test8(@RequestBody User user);          @PostMapping("test81")     public Response test81(@RequestParam("user") User user);          @PostMapping("test82")     public Response test82(User user);          @PostMapping("test9")     public Response> test9(@RequestBody List user); }

服务端

@RestController
public class SdkControler implements SdkTest {

    @Override
    public String test(String msg) {
        return msg;
    }

    @Data
    public static class User{

        @NotBlank(message = "{required}")
        private String name;

        @NotNull(message = "{required}")
        private Integer age;

    }
    
    @Override
    public Response test2(String msg) {
        return success(msg);
    }

    @Override
    public Response test3(String msg) {
        return success(msg);
    }

    @Override
    public Response test4(String msg) {
        return success(msg);
    }
    
    @Override
    public Response test41(String msg) {
        return success(msg);
    }

    @Override
    public Response> test5(String[] lists) {
        return success(Lists.newArrayList(lists));
    }

    @Override
    public Response test6(com.cqliving.cloud.sdk.SdkTest.User user) {
        return success(user);
    }

    @Override
    public Response test7(com.cqliving.cloud.sdk.SdkTest.User user) {
        return success(user);
    }

    @Override
    public Response test8(com.cqliving.cloud.sdk.SdkTest.User user) {
        return success(user);
    }
    
    @Override
    public Response test81(com.cqliving.cloud.sdk.SdkTest.User user) {
        return success(user);
    }
    
    @Override
    public Response test82(com.cqliving.cloud.sdk.SdkTest.User user) {
        return success(user);
    }
    
    @Override
    public Response> test9(List user) {
        return success(user);
    }
}

客户端:

@RestController
public class SdkTestController {

    @Autowired
    private SdkTestClient testClient;

    @GetMapping("test1")
    public String test(String msg) {
        return testClient.test(msg);
    }

    @GetMapping("test2")
    public Response test2(String msg) {
        return testClient.test2(msg);
    }

    @PostMapping("test3")
    public Response test3(String msg) {
        return testClient.test3(msg);
    }

    @PostMapping("test4")
    public Response test4(String msg) {
        return testClient.test4(msg);
    }
    @PostMapping("test41")
    public Response test41(String msg) {
        return testClient.test41(msg);
    }

    
    private String pattern = "[,,\\s|]";
    @PostMapping("test5")
    public Response> test5(String lists) {
        return testClient.test5(lists.split(pattern));
    }
    
    @GetMapping("test6")
    public Response test6(User user){
        return testClient.test6(user);
    }
    
    @GetMapping("test7")
    public Response test7(User user){
        return testClient.test7(user);
    }
    
    @PostMapping("test8")
    public Response test8(User user){
        return testClient.test8(user);
    }
    
    @PostMapping("test81")
    public Response test81(User user){
        return testClient.test81(user);
    }
    
    @PostMapping("test82")
    public Response test82(User user){
        return testClient.test82(user);
    }
    
    @PostMapping("test9")
    public Response> test9(@RequestBody List user){
        return testClient.test9(user);
    }
}

 

你可能感兴趣的:(java,springcloud,feign)