以往以JSON格式返回只需要在控制器方法上加上@RequestMapping就可以了,记录其他3个知识点
1.常规情况下对于属性值为null的转换为空字符
直接手写一个配置类Jackson,套模板,不用加乱七八糟的依赖包
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.io.IOException; @Configuration public class Jackson { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper; objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } }
然后对于控制器类中的方法就可以直接显示
2.JSONObject类的使用
(1)导入依赖包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency>
(2)语法
在返回json格式上没什么用,常规的也可以,主要是用来把toString方法转化为json格式的字符串,以json格式处理null,上面的模板也是够用的。
@RequestMapping("/json4") public JSONObject json4(){ JSONObject person4=new JSONObject();//原生JSONObject对象,和map差不多,只会显示设置的key person4.put("age",22); person4.put("hobbies",new String[] {"乒乓","骑行"}); System.out.println(person4); return person4; } @RequestMapping("/json5") public Person json5(){//实体类转为JSONObject Person person5=new Person(); person5.setAge(22); person5.setName("霸王别鸡"); person5.setHobbies(new String[]{"王者荣耀","羽毛球"}); System.out.println(person5); System.out.println(JSONObject.toJSON(person5)); System.out.println(JSONObject.toJSONString(person5)); return person5; } @RequestMapping("/json6") public JSONObject json6(){ HashMap<String,Object> map=new HashMap<>();//通过Map生成JSONObject map.put("name","灰太狼"); map.put("age",21); map.put("student",false); map.put("hobbies",new String[]{"发明","抓羊"}); System.out.println(JSONObject.toJSON(map)); return (JSONObject) JSONObject.toJSON(map); } @RequestMapping("json7") public JSONObject json7(){ String string="{ "name":"Mike","age":22 }";//通过字符串生成JSONObject //String string="{ "name":,,:"Mike","age":22 ,,,}";//乱来的话运行会报错 JSONObject jsonObject=JSONObject.parseObject(string); return jsonObject; } @RequestMapping("/json8") public JSON json8(){ ArrayList<Person> list=new ArrayList<Person>();//试一下JSON对象,一般都是用JSONObject的 list.add(person); Person person8=new Person(); person.setName("person8"); list.add(person8); JSON json= (JSON) JSON.toJSON(list); System.out.println(json); return json; } @RequestMapping("/json9") public JSONArray json9(){ JSONArray jsonArray = new JSONArray();//测一下JSONArray JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("name","守林鸟"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("name","灰太狼"); jsonArray.add(jsonObject1); jsonArray.add(jsonObject2); System.out.println(jsonArray); return jsonArray; }
3.自行封装统一返回的数据结构
就是下面这个东西
自定义一个定义一个类JsonResult
import lombok.Data; @Data public class JsonResult<T> { private T data; private String code; private String msg; public JsonResult() { this.code = "10086"; this.msg = "没有返回的数据"; } public JsonResult(T data) { this.data = data; this.code = "200"; this.msg = "有数据返回"; } }
测试方法
@RequestMapping("/json10") public JsonResult<Person> json10(){ return new JsonResult<>(); } @RequestMapping("/json11") public JsonResult<Person> json11(){ return new JsonResult<>(null); } @RequestMapping("/json12") public JsonResult<Person> json12(){ Person person12 = new Person(); person12.setName("person12"); return new JsonResult<>(person12); }
这里保留了上述处理null的Jackson类。
参考&引用
https://blog.csdn.net/taojin12/article/details/88244907