method GET must not have a request body

描述

使用feign声明式调用接口,代码如下


 /**
 * 通过多条件查询用户
 *
 * @param params 参数
 * @return response
 */
@GetMapping("/users")
Response<PageResponse<LinkedHashMap<String, Object>>> list( Map<String, String> params);

原因

  • feign在调用请求的时候,如果参数上没有指明参数是请求体参数还是请求行参数,默认认为是请求体
  • 项目中feign底层又使用的是okHttp,okHttp默认get方式请求不能有requestBody。源码如下
public Request.Builder method(String method, @Nullable RequestBody body) {
    if (method == null) {
        throw new NullPointerException("method == null");
    } else if (method.length() == 0) {
        throw new IllegalArgumentException("method.length() == 0");
    } else if (body != null && !HttpMethod.permitsRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must not have a request body.");
    } else if (body == null && HttpMethod.requiresRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must have a request body.");
    } else {
        this.method = method;
        this.body = body;
        return this;
    }
}

解决办法

参数上添加RequestParam注解


 /**
 * 通过多条件查询用户
 *
 * @param params 参数
 * @return response
 */
@GetMapping("/users")
Response<PageResponse<LinkedHashMap<String, Object>>> list(@RequestParam  Map<String, String> params);