一、创建一个springboot个项目
操作详情参考:1.springboo之helloword 快速搭建一个web项目
二、编写实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
* 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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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;
}
}
|
四、测试返回json数据
浏览器输入http://localhost:8080/user/getuser
得出结果:服务器是以json数据格式返回给浏览器
五、返回list到页面
5.1.返回数据的controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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;
}
}
|
5.2.得出结果
在浏览器访问 http://localhost:8080/user/getuserlist
六、返回map到浏览器
既然返回实体,和list的试验过了,那么再试验一下返回map类型的数据吧
6.1返回的controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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.hashmap;
import java.util.list;
import java.util.map;
/**
* created by cr7 on 2017-8-18 json返回数据的controller
*/
@restcontroller
@requestmapping ( "user" )
public class returnjsoncontroller {
@requestmapping ( "getusermap" )
public map<string,user> getusermap(){
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" );
map<string,user> map = new hashmap<>();
map.put( "user1" ,user1);
map.put( "user2" ,user2);
map.put( "user3" ,user3);
return map;
}
}
|
6.2得出的结果
在浏览器中访问http://localhost:8080/user/getusermap
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/zhanghaoliang/p/7389336.html