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. 清空表单方法 清空变量 iview modal

    方法一 通过json序列号和反序列号 清空一次数据 数据需要copy出来一份 orgFormClearAllInput () { this.$refs.n1.formValidate = JSON.p ...

  2. List去重复数据

    for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {  for ( int j = list.size() - 1 ; j > i; j -- ...

  3. [ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not <type 'str'>.

    错误信息 [ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not &l ...

  4. MySQL存储过程实践

    对employees数据库建立存储过程 创建不含有输入输出变量的存储过程 DELIMITER // -- 设定语句结束分隔符 DROP PROCEDURE IF EXISTS GetEmployees ...

  5. 五分钟掌握 for...in 和 for...of 区别

    GitHub 地址,欢迎star,查看更多整理的前端知识 for...in for...in 语句以任意顺序遍历一个对象的可枚举属性. for...in 遍历对象本身的所有可枚举属性,以及对象从其构造 ...

  6. Mysql 锁总结

    锁 部分总结参考博客 http://b.codejs.cc/articles/2017/10/23/1508749325215.html http://blog.csdn.net/cug_jiang1 ...

  7. Tiny4412 U-BOOT移植(转)

    http://blog.csdn.net/eshing/article/details/37520291(转) 一.移植前说明: 1.  特别声明:此文档是我的学习文档,里面肯定有错误地方,仅供参考! ...

  8. Android Studio修改默认字体大小

    安装Android Studio后,默认的字体很小,看着很不舒服,如下图 因此,我们需要改变字体大小,步骤如下: 一.打开设置 二.找到Font,这里系统的主题不能修改,我们点击Save As... ...

  9. 大数据学习——linux常用命令(一)

    一.基本日常操作命令 1 查看当前所在工作目录的全路径 pwd 2 查看当前系统的时间 date 设置时间,date -s"2018-11-12" 修改时间后,需要写入硬件bios ...

  10. xtu字符串 A. Babelfish

    A. Babelfish Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class ...