关于使用spring mvc时出现 HTTP Status 400
来源:互联网 发布:印章制作软件app 编辑:程序博客网 时间:2024/04/20 22:24
先记录一下这个错误
1、后台使用sring mvc的@RequestParam接受form表单提交的两个参数
2、使用Postman测试接口,可以看到在使用默认的form-data类型时出现了 HTTP Status 400 - Required String parameter ‘xx’ is not present 这个错误
3、更换成x-www-form-urlencoded类型时返回成功
4、如果使用@RequestBody接收参数,同@RequestParam一样,都是需要设置x-www-form-urlencoded类型才能正确返回。
再来分析一下这个错误的原因
1、先来看一下@RequestBody和@RequestParam的区别
@RequestParam
- A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况(由String到简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get方式中queryString的值,也可以处理post方式中 body data的值。
- B)用来处理Content-Type: 为application/x-www-form-urlencoded编码的内容,提交方式GET、POST。(不设置这个属性,这就是默认值)
- C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定。 在方法参数里面如是:public @ResponseBody JsonResultgetPublishedToposByConnStreamId(@RequestParam(value = “streamId”, required = false) String streamId) {}
@RequestBody
- A) GET、POST方式提时, 根据request header Content-Type的值来判断:application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam,@ModelAttribute也可以处理,当然@RequestBody也能处理);multipart/form-data, 不能处理(次类型多用来上传文件类型—即使用@RequestBody不能处理这种格式的数据,@RequestParam这个却是可以处理的。);其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
- B) PUT方式提交时, 根据request header Content-Type的值来判断:application/x-www-form-urlencoded, 必须;multipart/form-data, 不能处理;其他格式, 必须;说明:request的body部分的数据编码格式由header部分的Content-Type指定;
总结:
@RequestBody这个一般处理的是在ajax请求中声明contentType: “application/json; charset=utf-8”时候。也就是json数据或者xml(我没用过这个,用的是json)
@RequestParam这个一般就是在ajax里面没有声明contentType的时候,为默认的urlencode格式时,用这个。
2、spring mvc其他的参数绑定的注释
handler method 参数绑定常用的注解,一般根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)
- A、处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解: @PathVariable;
- B、处理request header部分的注解: @RequestHeader, @CookieValue;
- C、处理request body部分的注解:@RequestParam, @RequestBody;
- D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
3、关于http中的Content-Type
MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。
常见的媒体格式类型如下:
text/html : HTML格式text/plain :纯文本格式 text/xml : XML格式image/gif :gif图片格式 image/jpeg :jpg图片格式 image/png:png图片格式
以application开头的媒体格式类型:
application/xhtml+xml :XHTML格式 application/xml : XML数据格式 application/atom+xml :Atom XML聚合格式 application/json : JSON数据格式 application/pdf :pdf格式 application/msword : Word文档格式 application/octet-stream : 二进制流数据(如常见的文件下载) application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
另外一种常见的媒体格式是上传文件之时使用的:
multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
以上就是我们在日常的开发中,经常会用到的若干content-type的内容格式。
Spring MVC中关于关于Content-Type类型信息的使用:
- value: 指定请求的实际地址, 比如 /action/info之类。
- method:指定请求的method类型,GET、POST、PUT、DELETE等
- consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
- produces: 指定返回的内容类型,仅当request请求头中(Accept)类型中包含该指定类型才返回
- params:指定request中必须包含某些参数值是,才让该方法处理
- headers:指定request中必须包含某些指定的header值,才能让该方法处理请求
总结
一个比较细节的错误,主要是因为自己的一些知识盲点,对content-type和spring mvc参数绑定的不够了解,因为在网上也没有看到合理的说明,就自己记录一下,今后在开发中也要不断的发现问题,解决问题并且记录和总结问题。
- 关于使用spring mvc时出现 HTTP Status 400
- Spring MVC HTTP Status 406
- Spring MVC HTTP Status 405
- Spring MVC 遇到的问题http status 400
- Spring MVC 提示:HTTP Status 404 - /**/**
- Web Service 使用时出现 HTTP Status 401: Unauthorized
- jsp spring mvc 上传时HTTP Status 405 - Request method 'GET' not supported是什么原因
- HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
- spring mvc中设置返回的http status自定义编码
- 关于spring-mvc中使用ajax调用后台接口出现400 bad request的问题解决方案
- Spring MVC 访问时出现400错误
- spring mvc 数据绑定问题 提交表单提示HTTP status 400, The request sent by the client was syntactically incorrect
- spring mvc 数据绑定问题 提交表单提示HTTP status 400, The request sent by the client was syntactically incorrect
- Tomcat7出现HTTP Status 500
- Tomcat7出现HTTP Status 500
- struts hibernate spring 环境配置时出现 HTTP Status 404 Servlet action is not available (解决方法)
- Struts2当调用action时出现HTTP Status 404 问题解决
- spring http status 503 错误
- mysql tcpdump使用
- 惊!Facebook的AI机器人发展出了自己的语言,AI要觉醒吗?
- 1.twoSum
- mmap 详解
- LeetCode#44. Wildcard Matching
- 关于使用spring mvc时出现 HTTP Status 400
- 使用jquery的load方法设计动态加载,并解决被加载页面JavaScript失效问题
- python webhelp 类
- 使用sping cloud实现配置中心化
- 数据结构---内排序
- 系统辅助软件合集1
- T
- Cocos2d-x与OpenGL底层的感想
- 2.reverse