SpringBoot之返回json数据的实现方法

2025-05-29 0 86

一、创建一个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数据格式返回给浏览器

SpringBoot之返回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

SpringBoot之返回json数据的实现方法

六、返回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

SpringBoot之返回json数据的实现方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/zhanghaoliang/p/7389336.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot之返回json数据的实现方法 https://www.kuaiidc.com/110858.html

相关文章

发表评论
暂无评论