-
基本类型
@ResponseBody
@RequestMapping(value = "/baseType")
public String bastType(@RequestParam(value = "age",required = false,defaultValue = "1") Integer age){
return "age:"+age;
}
请求: /baseType?age=2
结果: age:20
-
数组
@ResponseBody
@RequestMapping(value = "/array")
public String array(String[] name){
StringBuilder sb = new StringBuilder();
for (String str : name) {
sb.append(str).append(",");
}
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
请求: /array?name=tom&name=jack&name=jim
结果: tom,jack,jim
-
对象
public class User {
private String name;
private Integer age;
// getter and setter...
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/object")
public String object(User user) {
return user.toString();
}
请求: /object?name=jack&age=10
结果: User{name='jack', age=20}
-
对象.对象
public class User {
private String name;
private Integer age;
private ContactInfo contactInfo;
// getter and setter...
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", contactInfo=" + contactInfo +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/object2")
public String object2(User user) {
return user.toString();
}
请求: /object2?name=jack&age=20&contactInfo.phone=123456&contactInfo.address=xxxxx
结果: User{name='jack', age=20, contactInfo=ContactInfo{phone='123456', address='xxxxx'}}
-
同属性多对象
public class Admin {
private String name;
private Integer age;
// getter and setter...
@Override
public String toString() {
return "Admin{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class User {
private String name;
private Integer age;
// getter and setter
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/object3")
public String object2(User user,Admin admin) {
return user.toString()+ admin.toString();
}
@InitBinder("user")
public void initUser(WebDataBinder binder){
binder.setFieldDefaultPrefix("user.");
}
@InitBinder("admin")
public void initAdmin(WebDataBinder binder){
binder.setFieldDefaultPrefix("admin.");
}
请求: /object3?user.name=jack&age=20&admin.name=tom
结果: User{name='jack', age=20}Admin{name='tim', age=20}
-
list
import java.util.List;
public class UserListForm {
private List<User> users ;
// getter and setter...
@Override
public String toString() {
return "UserListForm{" +
"users=" + users +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/list")
public String list(UserListForm userListForm) {
return userListForm.toString();
}
请求: /list?users[0].name=111&users[0].age=1&users[1].name=222&users[1].age=2
结果: UserListForm{users=[User{name='111', age=1, contactInfo=null}, User{name='222', age=2, contactInfo=null}]}
说明: users[index] 中index的索引不要跳跃 如果这样写/list?users[0].name=111&users[0].age=1&users[3].name=222&users[3].age=2
结果会是这样的:UserListForm{users=[User{name='111', age=1, contactInfo=null}, User{name='null', age=null, contactInfo=null}, User{name='null', age=null, contactInfo=null}, User{name='222', age=2, contactInfo=null}]}
其他方法: 也可以在前端把数据组装成json字符串,然后传入,再用工具转化为对象
-
set
import java.util.LinkedHashSet;
import java.util.Set;
public class UserSetForm {
//set需要事先初始化一下才行,而且要先设置好里面的对象,也不能索引越界
private Set<User> users;
public UserSetForm(){
users = new LinkedHashSet<>();
users.add(new User());
users.add(new User());
}
// getter and setter...
@Override
public String toString() {
return "UserSetForm{" +
"users=" + users +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/set")
public String set(UserSetForm userSetForm) {
return userSetForm.toString();
}
请求: /set?users[0].name=111&users[0].age=1&users[1].name=222&users[1].age=2
结果: UserSetForm{users=[User{name='111', age=1, contactInfo=null}, User{name='222', age=2, contactInfo=null}]}
说明: set需要事先初始化一下才行,而且要先设置好里面的对象,也不能索引越界 set用来去重,需要重写hashCode()equals()方法
-
map
public class UserMapForm {
private Map<String,User> users;
//getter and setter
@Override
public String toString() {
return "UserMapForm{" +
"users=" + users +
'}';
}
}
@ResponseBody
@RequestMapping(value = "/map")
public String map(UserMapForm userMapForm) {
return userMapForm.toString();
}
请求: /map?user[x].name=tom&user[x].age=20&user[y].name=jack&user[y].age=22
结果: UserMapForm{users={x=User{name='tom', age=20, contactInfo=null}, y=User{name='jack', age=22, contactInfo=null}}}
-
json
/**
* 需要的jar包
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
request-header Content-Type:application/json
data:
{
"name":"aaaa",
"age":20
}
* @param user
* @return
*/
@ResponseBody
@RequestMapping(value = "/json")
public String json(@RequestBody User user) {
return user.toString();
}
请求: localhost:8085/json
结果: User{name='aaaa', age=20, contactInfo=null}
-
xml
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="admin")
public class Admin {
private String name;
private Integer age;
@XmlElement(name="age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@XmlElement(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Admin{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 所需jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
request header Content-Type:application/xml
data:
<admin>
<name>aaa</name>
<age>20</age>
</admin>
* @param admin
* @return
*/
@ResponseBody
@RequestMapping(value = "/xml")
public String xml(@RequestBody Admin admin) {
return admin.toString();
}
请求: localhost:8085/xml
结果: Admin{name='aaa', age=20}