经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
POST不同提交方式对应的Content-Type,及java服务器接收参数方式
来源:cnblogs  作者:栗子欧  时间:2018/11/2 9:19:53  对本文有异议

POST不同提交方式对应的Content-Type,及java服务器接收参数方式

注:本博客参考了网上的文章结合自己工作总结后所写,主要用于记录自己工作所得,如有错误请批评指正.

简介:

Content-Type(MediaType),即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息.参考

response.Header里常见Content-Type一般有以下四种:

  • application/x-www-form-urlencoded
  • multipart/form-data
  • application/json
  • text/xml

详解:

1.application/x-www-form-urlencoded

application/x-www-form-urlencoded是最常见的Content-Type,form表单默认提交方式对应的content-type.

当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串追加到url后面,用?分割,加载这个新的url.
当action为post,且表单中没有type=file类型的控件时,Content-Type也将采用此编码方式,form数据将以key:value键值对的方式传给server.

表单提交:

  1. ```
  2. <form action="/test" method="post">
  3. <input type="text" name="name" value="zhangsan">
  4. <input type="text" name="age" value="12">
  5. <input type="text" name="hobby" value="football">
  6. </form>
  7. ```

后台:

  1. import java.io.Serializable;
  2. public class Student implements Serializable {
  3. private String name;
  4. private String hobby;
  5. private int age;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public String getHobby() {
  13. return hobby;
  14. }
  15. public void setHobby(String hobby) {
  16. this.hobby = hobby;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. }
  1. @RequestMapping(value = "/test", method = RequestMethod.POST)
  2. public String test(Student student) {
  3. System.out.println(student.getName());
  4. return "/test1";
  5. }

2.multipart/form-data

当post表单中有type=file控件时content-type会使用此编码方式.

表单提交:

  1. ```
  2. <form action="/test" method="post" enctype="multipart/form-data">
  3. <input type="text" name="name" value="zhangsan">
  4. <input type="text" name="age" value="12">
  5. <input type="text" name="hobby" value="football">
  6. <input type="file" name="file1"
  7. </form>
  8. ```

后台:

  1. @RequestMapping(value = "/test", method = RequestMethod.POST)
  2. public String test(Student student,@RequestParam(value = "file1", required = false) MultipartFile file1) {
  3. System.out.println(student.getName());
  4. return "/test1";
  5. }

3.application/json

随着json规范的流行,以及前后端分离趋势所致,该编码方式被越来越多人接受.
前后端分离后,前端通常以json格式传递参数,因此该编码方式较适合RestfulApi接口.

前端传参:

  1. $.ajax({
  2. url: '/test',
  3. type: 'POST',
  4. data: {
  5. "name": "zhangsan",
  6. "age": 12,
  7. "hobby": "football"
  8. },
  9. dataType: "json",
  10. success: function (date) {
  11. }
  12. })

后台:

  1. @RequestMapping(value = "/test", method = RequestMethod.POST)
  2. public String test(@RequestBody Student student) {
  3. System.out.println(student.getName());
  4. return "/test1";
  5. }

4.text/xml

XML-RPC(XML Remote Procedure Call)。它是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范。

soapUI等xml-rpc请求的参数格式.

提交参数:

  1. <arg0>
  2. <name>zhangsan</name>
  3. <age>12</age>
  4. <hobby>footbal</hobby>
  5. </arg0>
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号