POST不同提交方式对应的Content-Type,及java服务器接收参数方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了POST不同提交方式对应的Content-Type,及java服务器接收参数方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2756字,纯文字阅读大概需要4分钟。
内容图文
![POST不同提交方式对应的Content-Type,及java服务器接收参数方式](/upload/InfoBanner/zyjiaocheng/846/316ecd05bb39404caa5da2ef396ae346.jpg)
简介:
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.
表单提交:
<form action="/test" method="post">
<input type="text" name="name" value="zhangsan">
<input type="text" name="age" value="12">
<input type="text" name="hobby" value="football">
</form>
后台:
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private String hobby;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(Student student) {
System.out.println(student.getName());
return "/test1";
}
2.multipart/form-data
当post表单中有type=file控件时content-type会使用此编码方式.
表单提交:
<form action="/test" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="zhangsan">
<input type="text" name="age" value="12">
<input type="text" name="hobby" value="football">
<input type="file" name="file1"
</form>
后台:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(Student student,@RequestParam(value = "file1", required = false) MultipartFile file1) {
System.out.println(student.getName());
return "/test1";
}
3.application/json
随着json规范的流行,以及前后端分离趋势所致,该编码方式被越来越多人接受.
前后端分离后,前端通常以json格式传递参数,因此该编码方式较适合RestfulApi接口.
前端传参:
$.ajax({
url: '/test',
type: 'POST',
data: {
"name": "zhangsan",
"age": 12,
"hobby": "football"
},
dataType: "json",
success: function (date) {
}
})
后台:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody Student student) {
System.out.println(student.getName());
return "/test1";
}
4.text/xml
XML-RPC(XML Remote Procedure Call)。它是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范。
soapUI等xml-rpc请求的参数格式.
提交参数:
<arg0>
<name>zhangsan</name>
<age>12</age>
<hobby>footbal</hobby>
</arg0>
分类: java
内容总结
以上是互联网集市为您收集整理的POST不同提交方式对应的Content-Type,及java服务器接收参数方式全部内容,希望文章能够帮你解决POST不同提交方式对应的Content-Type,及java服务器接收参数方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。