java spring boot返回json的写法
java spring boot返回json的写法 (用于写接口)
加了
@RestController 控制器的返回值就会返回json了
package com.example.demo2122; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.*; @RestController public class HelloControl { @RequestMapping("/hello") public Map<Object, Object> hello(@RequestParam(value = "name", defaultValue = "World") String name) { List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>(); Map<Object, Object> map1 = new HashMap<Object, Object>(); map1.put("姓名1", "张三"); map1.put("姓名2", "李四"); map1.put("姓名3", "王五"); list.add(map1); Map<Object, Object> map=this.commonajax(100,"查询成功",list); return map; } private Map<Object, Object> commonajax(Integer code,String msg,List<Map<Object, Object>> data){ Map<Object, Object> map = new HashMap<Object, Object>(); map.put("code", code); map.put("msg", "查询成功"); map.put("data", data); return map; } }
如果遇到什么不懂的地方直接关注公众号留言(本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。)
作者:newmiracle
出处:https://www.cnblogs.com/newmiracle/