/**
* created by cr7 on 2017-8-18 返回json数据实体类
*/
public class user {
private int id;
private string username;
private string password;
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
}
三、编写控制层controller类
import com.example.bean.user;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* created by cr7 on 2017-8-18 json返回数据的controller
*/
@restcontroller
@requestmapping("user")
public class returnjsoncontroller {
@requestmapping("getuser")
public user getuser(){
user user = new user();
user.setid(1);
user.setusername("zhanghaoliang");
user.setpassword("1231");
return user;
}
}
package com.example.demo;
import com.example.bean.user;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import java.util.arraylist;
import java.util.list;
/**
* created by cr7 on 2017-8-18 json返回数据的controller
*/
@restcontroller
@requestmapping("user")
public class returnjsoncontroller {
@requestmapping("getuserlist")
public list<user> getuserlist(){
user user1 = new user();
user1.setid(1);
user1.setusername("zhanghaoliang");
user1.setpassword("123");
user user2 = new user();
user2.setid(2);
user2.setusername("chensi");
user2.setpassword("456");
user user3 = new user();
user3.setid(3);
user3.setusername("doudou");
user3.setpassword("789");
list<user> list = new arraylist<>();
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
}