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