目 录CONTENT

文章目录

spring mvc返回map的情况回顾

在水一方
2022-02-17 / 0 评论 / 0 点赞 / 194 阅读 / 1,641 字 / 正在检测是否收录...

印象中平时在springmvc中比较少返回map的json数据,有点忘记了返回map时的json数据格式是怎样的,写这篇文章回顾一下,以便如果遇到返回map的json数据时能快速识别

返回List<Map<String ,User>>

  @GetMapping("list")
    public List<Map<String ,User>> dmeo2(){
      List<Map<String,User>> list =new ArrayList<>();
        Map<String, User> map = new HashMap();
        User user = new User();
        user.setUserName("zhangsan");
        User user1 = new User();
        user1.setUserName("beijing");
        map.put("name", user);
        map.put("address", user1);
        list.add(map);
        return list;

    }
}

public class User implements Serializable {

    private static final long serialVersionUID = 7398333692719311866L;
    private String userName;
    public User(){

    }
    public User(String str){
        this.userName = str;

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

json数据格式样式

[{"address":{"userName":"beijing"},"name":{"userName":"zhangsan"}}]

返回map数据

 @GetMapping("map")
    public Map<String ,User> dmeo1(){
        Map<String, User> map = new HashMap();
        User user = new User();
        user.setUserName("zhangsan");
        User user1 = new User();
        user1.setUserName("beijing");
        map.put("name" ,user);
        map.put("address" ,user1);
        return map;

    }


public class User implements Serializable {
    private static final long serialVersionUID = 7398333692719311866L;
    private String userName;
    public User(){
    }
    public User(String str){
        this.userName = str;

    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
{"address":{"userName":"beijing"},"name":{"userName":"zhangsan"}}
0

评论区