一、創建一個springBoot個項目
創新互聯-專業網站定制、快速模板網站建設、高性價比和政網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式和政網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋和政地區。費用合理售后完善,十載實體公司更值得信賴。
操作詳情參考:1.SpringBoo之Helloword 快速搭建一個web項目
二、編寫實體類
/**
* 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;
}
}四、測試返回Json數據
瀏覽器輸入http://localhost:8080/user/getUser
得出結果:服務器是以json數據格式返回給瀏覽器

五、返回list到頁面
5.1.返回數據的controller
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
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數據的實現方法
分享路徑:http://www.yijiale78.com/article26/gipscg.html
成都網站建設公司_創新互聯,為您提供品牌網站建設、定制開發、全網營銷推廣、微信小程序、移動網站建設、虛擬主機
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯