curl -X POST 'http://localhost:8080/formPost' -d 'id=1&name=foo&mobile=13612345678'
//org.springframework.web.method.annotation.RequestParamMethodArgumentResolver#resolveName if (arg == null) { String[] paramValues = webRequest.getParameterValues(name); if (paramValues != null) { arg = paramValues.length == 1 ? paramValues[0] : paramValues; } }
curl -X POST -H "Content-Type: application/json" 'http://localhost:8080/jsonPost' -d '{"id":2,"name":"foo","mobile":"13656635451"}'
//com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#readInternal protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream in = inputMessage.getBody(); byte[] buf = new byte[1024]; while(true) { int bytes = in.read(buf); if(bytes == -1) { byte[] bytes1 = baos.toByteArray(); return JSON.parseObject(bytes1, 0, bytes1.length, this.charset.newDecoder(), clazz, new Feature[0]); } if(bytes > 0) { baos.write(buf, 0, bytes); } } }
web層代碼java
@RequestMapping(value="/mixPost", method=RequestMethod.POST ) public Result<Void> mixPostTest(@RequestBody @Valid Foo foo, @RequestParam Integer sex)
提交請求web
curl -X POST -H "Content-Type: application/json" 'http://localhost:8080/mixPost?sex=1' -d '{"id":2,"name":"foo","mobile":"13656635451"}'
@RequestMapping(value="/formPost", method=RequestMethod.POST ) public Result<Void> formPostTest(@RequestParam int id, @RequestParam String name, @RequestParam String mobile)
由於id是必填參數 若是請求參數中不含id的話 會報錯 以下所示spring
org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'id' is not present at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255) at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:95) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
經過此方法能夠快速定位到源碼json
@RequestMapping(value="/jsonPost", method=RequestMethod.POST ) public Result<Void> jsonPostTest(@RequestBody @Valid Foo foo)
由於確定要先構造一個空Foo對象 而後才能注入各屬性值 因此在Foo的無參構造函數中加斷點, 能夠定位到json請求解析參數的源碼app