码迷,mamicode.com
首页 > 编程语言 > 详细

1、SpringBoot bean,list,map Json返回

时间:2019-04-29 20:58:07      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:www.   private   use   this   class   mic   hashmap   图片   src   

1、Bean

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;
    }
}
@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;
    }
}

http://localhost:8080/user/getUser
技术图片

2、List

@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;
    }
}

http://localhost:8080/user/getUserList
技术图片

3、Map

@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;
    }
}

http://localhost:8080/user/getUserMap
技术图片

转发https://www.cnblogs.com/zhanghaoliang/p/7389336.html

1、SpringBoot bean,list,map Json返回

标签:www.   private   use   this   class   mic   hashmap   图片   src   

原文地址:https://www.cnblogs.com/xidianzxm/p/10792489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!