Jackson说明
Jackson说明
package com.stono.sboot2_chp4_jackson.controller; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.stono.sboot2_chp4_jackson.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller public class DataBindController { @Qualifier("getObjectMapper") @Autowired ObjectMapper objectMapper; @RequestMapping("/updateUsers.json") @ResponseBody public String updateUsers(@RequestBody List<User> list) { StringBuilder builder = new StringBuilder(); for (User user : list) { builder.append(user.getName()).append(" "); } return builder.toString(); } @RequestMapping("/customize.json") @ResponseBody public String costomize() throws IOException { String jsonInput = "[{\"name\":\"stono\",\"id\":1},{\"name\":\"tom\",\"id\":2}]"; JavaType type = getCollectionType(List.class, User.class); List<User> users = objectMapper.readValue(jsonInput, type); return String.valueOf(users.size()); } public JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); } @JsonView(User.IdView.class) @RequestMapping("/id.json") @ResponseBody public User queryIds() { User user = new User(); user.setId(1); user.setName("stono"); return user; } @RequestMapping("/user.json") @ResponseBody public User user() { User user = new User(); user.setId(1); user.setName("stono"); return user; } @RequestMapping("/dept.json") @ResponseBody public Department getDepartment() { return new Department(1); } class Department{ Map map = new HashMap(); private int id; public Department(int id) { this.id = id; map.put("newAttr", 1); } @JsonAnyGetter public Map<String, Object> getOtherProperties() { return map; } public int getId() { return id; } public void setId(int id) { this.id = id; } } }
package com.stono.sboot2_chp4_jackson.controller; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.io.StringWriter; @Controller @RequestMapping("/stream") public class JacksonStreamController { @Qualifier("getObjectMapper") @Autowired ObjectMapper objectMapper; @RequestMapping("/parser.json") @ResponseBody public String parser() throws IOException { String json = "{\"name\":\"stono\",\"id\":1}"; JsonFactory f = objectMapper.getFactory(); String key = null, value = null; JsonParser parser = f.createParser(json); JsonToken token = parser.nextToken(); System.out.println("token1:"+token); token = parser.nextToken(); System.out.println("token2:"+token); if (token == JsonToken.FIELD_NAME) { key = parser.currentName(); } token = parser.nextToken(); System.out.println("token3:"+token); value = parser.getValueAsString(); parser.close(); return key+","+value; } @RequestMapping("/generator.html") @ResponseBody public String generate() throws IOException { JsonFactory f = objectMapper.getFactory(); StringWriter sw = new StringWriter(); JsonGenerator g = f.createGenerator(sw); g.writeStartObject(); g.writeStringField("name","stono"); // g.writeString("string"); g.writeEndObject(); g.close(); return sw.toString(); } }