今天收到同事反馈的问题。
Caused by: feign.FeignException$BadRequest: [400 Bad Request] during
[POST] to
[http://gateway-v2-se-java/meta/metaClass/metaGetAllChildren?className=Dialog¤tClass=false]
[MetaClassRemote#metaGetAllChildren(String,Boolean)]: [<!doctype
html>HTTP Status 400 – Bad<br/> Request
出问题的源代码:
@RequestMapping(value = "/test", headers = "{Content-Type=application/json;charset=UTF-8}", method = RequestMethod.POST)
Resp<List<CommonVo>> getChildren(@RequestParam("name") String name, @RequestParam(required = false, value = "currentName") Boolean currentName);
造成feign调用产生400 bad request的原因有很多,一般遇到的以下几种:
- 客户端请求参数和服务端请求参数不一致。
- 请求的header过大或参数过大
- 请求的header本身有问题
分别的对应解决方案有:
4. 修改源代码,让请求参数一致。
5. 设置以下参数:
server:
port: 8080
max-http-header-size: 102400 #调大服务提供者的header参数(微服务较多 不太适用)
- header本身的问题,通常在上个方案中已经包含,但是还有其他原因。
比如,我遇到的问题是代码中的header参数格式写错了。
注意这里的双引号,正确写法应该是:
headers = {
"Content-Type=application/json;charset=UTF-8"}
有问题,还是要先从源代码查起。