SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法
一、創(chuàng)建一個springBoot個項目
操作詳情參考:1.SpringBoo之Helloword 快速搭建一個web項目
二、編寫實體類
/** * 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類
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ù)格式返回給瀏覽器
五、返回list到頁面
5.1.返回數(shù)據(jù)的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返回數(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
六、返回map到瀏覽器
既然返回實體,和list的試驗過了,那么再試驗一下返回Map類型的數(shù)據(jù)吧
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返回數(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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot整合Web開發(fā)之Json數(shù)據(jù)返回的實現(xiàn)
- SpringBoot前后端json數(shù)據(jù)交互的全過程記錄
- SpringBoot實現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式
- SpringBoot響應(yīng)Json數(shù)據(jù)亂碼通過配置的解決
- springboot 返回json格式數(shù)據(jù)時間格式配置方式
- SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法
- SpringBoot響應(yīng)處理之以Json數(shù)據(jù)返回的實現(xiàn)方法
相關(guān)文章
SpringBoot中配置Web靜態(tài)資源路徑的方法
這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09基于surging跨網(wǎng)關(guān)跨語言進行緩存降級的問題小結(jié)
surging是一款開源的微服務(wù)引擎,包含了rpc服務(wù)治理,中間件,以及多種外部協(xié)議來解決各個行業(yè)的業(yè)務(wù)問題,這篇文章主要介紹了如何基于surging跨網(wǎng)關(guān)跨語言進行緩存降級,需要的朋友可以參考下2024-05-05json如何解析混合數(shù)組對象到實體類的list集合里去
這篇文章主要介紹了json解析混合數(shù)組對象到實體類的list集合里去的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例
這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽器及創(chuàng)建線程,文中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-06-06Java+opencv3.2.0實現(xiàn)hough直線檢測
這篇文章主要為大家詳細介紹了Java+opencv3.2.0之hough直線檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02