一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

2021-06-22 13:04張昊亮 Java教程

這篇文章主要介紹了SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、創(chuàng)建一個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數(shù)據(jù)實體類
 */
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返回數(shù)據(jù)的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數(shù)據(jù)

瀏覽器輸入http://localhost:8080/user/getuser

得出結(jié)果:服務(wù)器是以json數(shù)據(jù)格式返回給瀏覽器

SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

五、返回list到頁面

5.1.返回數(shù)據(jù)的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返回數(shù)據(jù)的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.得出結(jié)果

在瀏覽器訪問 http://localhost:8080/user/getuserlist

SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

六、返回map到瀏覽器

既然返回實體,和list的試驗過了,那么再試驗一下返回map類型的數(shù)據(jù)吧

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返回數(shù)據(jù)的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得出的結(jié)果

在瀏覽器中訪問http://localhost:8080/user/getusermap

SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品国产高清自在线看超 | 四虎精品在线观看 | 亚洲久草在线 | 国产91精选在线观看麻豆 | 99视频一区 | 国产最强大片免费视频 | 色偷偷亚洲综合网亚洲 | 日韩播放| 亚洲va精品中文字幕 | 母乳在线 | 黄色大片三级 | 天天综合天天色 | 久久亚洲电影www电影网 | 亚洲成年 | 日韩高清一区二区 | 国产亚洲女在线精品 | 国产亚洲毛片在线 | 精品一区二区三区在线视频观看 | 国产国语videosex另类 | chaopeng在线观看 | 国产小视频在线免费观看 | 四虎精品成人免费观看 | 色亚州| 亚洲大尺码 | 第四色男人天堂 | 亚洲天堂色视频 | 公园暴露娇妻小说 | 99热在线获取最新地址 | 九九精品视频在线免费观看 | 亚洲欧美一区二区三区在饯 | 任你操视频在线观看 | 欧美国产日产精品免费视频 | 欧美日韩视频一区三区二区 | 4虎影院永久地址www | 久久精品国产视频澳门 | 草草线在成年免费视频网站 | 国产一区二区三区久久小说 | 我与肥熟老妇的性事 | 免费370理论片中文字幕 | 亚洲日本va午夜中文字幕 | 美国雪白人妖sarina |