當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Eclipse中新建SpringBoot项目完成对json、pojo、map、list的请求
生活随笔
收集整理的這篇文章主要介紹了
Eclipse中新建SpringBoot项目完成对json、pojo、map、list的请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
Eclipse中新建SpringBoot項目并請求json數據返回HelloWorld見:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/88359953
實現
新建poji包,并新建User
package com.example.demo.pojo;public class User {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}完善Controller
package com.example.demo.controller;import java.util.ArrayList; import java.util.HashMap; import java.util.List;import javax.print.attribute.HashAttributeSet;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.example.demo.pojo.User; import com.sun.javafx.collections.MappingChange.Map;/*** @author badao* @Description:測試* @Time:2019年3月6日 下午11:20:19*/ @RestController public class UserController {/*** 返回基本類型json數據* @return*/@RequestMapping("hello")public String showhello() {return "hello world,badao Spring Boot";}/*** 返回pojo對象* @return*/@RequestMapping("pojo")public User showUser() {User user = new User();user.setId(1);user.setUsername("霸道");user.setPassword("123");return user;}/*** 返回Map集合對象* @return*/@RequestMapping("maps")public HashMap<String, Object> showMaps() {HashMap<String, Object> maps = new HashMap<String,Object>();maps.put("username","霸道");maps.put("password", "123");maps.put("mapkey", "mapvalue");return maps;}/*** 返回List集合對象* @return*/@RequestMapping("list")public List<User> showList() {List<User> list =new ArrayList<User>();User user1 =new User();user1.setId(1);user1.setUsername("badao");user1.setPassword("123");User user2 =new User();user2.setId(1);user2.setUsername("badao");user2.setPassword("123");list.add(user1);list.add(user2);return list;} }運行結果
打開瀏覽器輸入:
http://localhost:8080/pojo
?
http://localhost:8080/maps
http://localhost:8080/list
?
源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11005557
總結
以上是生活随笔為你收集整理的Eclipse中新建SpringBoot项目完成对json、pojo、map、list的请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse中新建SpringBoot
- 下一篇: SpringBoot中使用@Mapper