springcloud: 3 ways to solve the feign call parameter is the GET interface of the entity class

0 Preface

Recently, I was explaining the inter-group call of feign. I encountered the problem that the GET request with the call parameter as the entity class reported an error or the parameter was obtained as empty, so I recorded the solution uniformly for your subsequent reference.

1. Reason

First of all, we need to know that feign supports post requests by default for entity class parameters. Directly calling the GET-type entity class parameter interface will generate a 405 error or the parameter is empty, as shown below

GET interface:

    @GetMapping("getByBody2")
    public String getByBody2(Product product){
    
    
        return "product:" + JSON.toJSONString(product);
    }

Feign calling code:

@GetMapping("getByBody2")
String getByBody2(Product product

Error content:
insert image description here

This is because feign itself does not support this form of calling. He recommends converting to the POST interface, but sometimes, we cannot modify the original interface. When this kind of calling is required, we have to solve it, so let's look at the following three solutions

2. Solve

Method 1: Transfer parameter to map

First of all, we can't call because entity class parameters are not supported, so we can call by converting to key-value pair parameters

Parameter conversion:

@GetMapping("getBody2")
    public String getBody2(){
    
    
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        // 实体类转json字符串 需要引入fastjson依赖
        String jsonString = JSON.toJSONString(product);
        // json字符串转map
        Map params = JSON.parseObject(jsonString, Map.class);
        return "order info " + productFeignNacos.getByBody2(params);
    }

feign interface declaration:
pay attention to this form, you need to add the @RequestParam annotation, and name it consistent with the name of the called interface parameter

@GetMapping("getByBody2")
String getByBody2(@RequestParam("product") Map product);

Called interface:

    @GetMapping("getByBody2")
    public String getByBody2(Product product){
    
    
        return "product:" + JSON.toJSONString(product);
    }

Method 2: Use @SpringQueryMapannotations to automatically convert maps

@SpringQueryMapAnnotations can automatically convert entity classes to maps, so we don't need to manually convert them to maps

feign interface declaration

@GetMapping("getByBody2")
String getByBody3(@SpringQueryMap Product product);

Method 3: Utilize feign-httpclientToolkit

feign-httpclientThe toolkit provides an implementation that supports GET entity parameters, we only need to introduce this dependency

<dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>10.10.1</version>
</dependency>

Caller code:

    @GetMapping("getBody4")
    public String getBody4(){
    
    
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        return "order info " + productFeignNacos.getByBody4(product);
    }

feign interface declaration:

@GetMapping(value = "")
String getByBody4(Product product);

Called interface:

   @GetMapping(value = "getByBody3", consumes = MediaType.APPLICATION_JSON_VALUE)
    public String getByBody3(@RequestBody Product product){
    
    
        return "product:" + JSON.toJSONString(product);
    }

Call result display

insert image description here

Summarize

As above, three ways, you can choose

Guess you like

Origin blog.csdn.net/qq_24950043/article/details/129414457