POST不同提交方式对应的Content-Type,及java服

2018-11-28  本文已影响0人  哦00

简介:

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.

表单提交:

```     ```

后台:

importjava.io.Serializable;publicclassStudentimplementsSerializable{privateString name;privateString hobby;privateintage;publicStringgetName(){returnname;    }publicvoidsetName(String name){this.name = name;    }publicStringgetHobby(){returnhobby;    }publicvoidsetHobby(String hobby){this.hobby = hobby;    }publicintgetAge(){returnage;    }publicvoidsetAge(intage){this.age = age;    }}

@RequestMapping(value="/test", method = RequestMethod.POST)publicStringtest(Student student){        System.out.println(student.getName());return"/test1";    }

2.multipart/form-data

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

表单提交:

```   ```

后台:

@RequestMapping(value="/test", method = RequestMethod.POST)publicStringtest(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)publicStringtest(@RequestBody Student student){        System.out.println(student.getName());return"/test1";    }

4.text/xml

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

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

提交参数:

zhangsan12footbal

分类: java                                                                                                                                                                                  欢迎工作一到五年的Java工程师朋友们加入Java群: 891219277群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

上一篇下一篇

猜你喜欢

热点阅读