zoukankan      html  css  js  c++  java
  • 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();
    
        }
    }
  • 相关阅读:
    vbscript错误代码及对应解释大全(希望还没过时)
    对象存储服务MinIO安装部署分布式及Spring Boot项目实现文件上传下载
    一道算法题,求更好的解法
    浅谈SQLite——实现与应用
    Linux网络协议栈(二)——套接字缓存(socket buffer)
    服务器开发入门——理解异步I/O
    理解MySQL——复制(Replication)
    线性时间排序算法
    Linux网络协议栈(一)——Socket入门(2)
    理解MySQL——索引与优化
  • 原文地址:https://www.cnblogs.com/stono/p/8691335.html
Copyright © 2011-2022 走看看