当前位置: 移动技术网 > IT编程>开发语言>Java > POST不同提交方式对应的Content-Type,及java服务器接收参数方式

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

2018年11月01日  | 移动技术网IT编程  | 我要评论

卡通站,日本官方对亚投行作出表态,lgp970论坛

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.

表单提交:

 ```
 <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>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网