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

Jackson说明的更多相关文章

  1. jackson简单使用,对象转json,json转对象,json转list

    添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile g ...

  2. Jackson 通过自定义注解来控制json key的格式

    Jackson 通过自定义注解来控制json key的格式 最近我这边有一个需求就是需要把Bean中的某一些特殊字段的值进行替换.而这个替换过程是需要依赖一个第三方的dubbo服务的.为了使得这个转换 ...

  3. Jackson的简单用法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1简介 Jackson具有比较高的序列化和反序列化效率,据测试,无论是 ...

  4. jackson error 含义log

    1. 反序列化失败,类型不匹配 Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserial ize ...

  5. jackson annotations注解详解

    jackson中常用到的注解 猛击下面的连接地址 http://blog.csdn.net/sdyy321/article/details/40298081

  6. jackson官方快速入门文档

    官方地址: http://jackson.codehaus.org/ http://wiki.fasterxml.com/JacksonInFiveMinutes http://wiki.faster ...

  7. Jackson将json字符串转换成泛型List

    Jackson,我感觉是在Java与Json之间相互转换的最快速的框架,当然Google的Gson也很不错,但是参照网上有人的性能测试,看起来还是Jackson比较快一点 Jackson处理一般的Ja ...

  8. 使用Jackson解析Json示例

    原文http://blog.csdn.net/gebitan505/article/details/17005735 custom.json: {     "country":&q ...

  9. Json解析工具Jackson(使用注解)

    原文http://blog.csdn.net/nomousewch/article/details/8955796 接上一篇文章Json解析工具Jackson(简单应用),jackson在实际应用中给 ...

  10. Json解析工具Jackson(简单应用)

    原文http://blog.csdn.net/nomousewch/article/details/8955796 概述 Jackson库(http://jackson.codehaus.org),是 ...

随机推荐

  1. js 数组过滤 filter

    let res = this.list.filter(item => routeEqual(this.currentRouteObj, item) || item.name === this.$ ...

  2. 20道必须掌握的C++面试题

    20道必须掌握的C++面试题 在面试C++方面的工作时,经常会遇到各种面试题,这对应聘人员的知识掌握能力要求较高.本文将为大家带来的就是20道必须掌握的C++面试题,不要错过哦! 问1:请用简单的语言 ...

  3. STL || Gym 101653U Top 25

    一组字符串给出两种排列方式, 求最小分成多少组 如 A     A B     C C    D D    B E    E 则分成3组 A B C D E 即为1 3 1 #include < ...

  4. modify django app models.py adn settings.py

    from django.db import models from django.contrib import admin # from personal import models class Us ...

  5. SQL 中 NOT IN 查询不到数据

    一.问题 用以下sql语句查询数据,结果为空 SELECT a.ID , a.Sub_Project_Name , a.Sub_Project_Type FROM TB_KYSubProject a ...

  6. IOS学习笔记3—Objective C—简单的内存管理

    今天简述一下简单的内存管理,在IOS5.0以后Apple增加了ARC机制(Automatic Reference Counting),给开发人员带来了不少的方便,但是为了能更好的理解IOS内存管理机制 ...

  7. 695. Max Area of Island@python

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. nginx解决跨域(前后端分离)

    Nginx解决跨域问题 后端接口 请求地址 返回数据(json数据) http://127.0.0.1:8080//app Hello World! 前端代码 通过nginx做静态资源服务器访问端口8 ...

  9. MySQL数据库常见面试题

    什么是存储过程?有哪些优缺点? 存储过程简单来说就是为了以后使用而保存的一条或多条预编译SQL语句,这些语句块像一个方法一样执行一些功能. 优点: 类似于封装,简化操作: 不用反复建立一系列处理步骤, ...

  10. python实现快速排序、冒泡

    快速排序:首先任意选取一个数据(通常选用数组的第一个参数)作为关键数据,然后将比它小的数放在它前面,比它大的数放在后面,这个过程称之为快速排序 def quick_sort(l): if len(l) ...