当前位置:首页 > 开发 > 编程语言 > Json > 正文

JsonObjectMapper

发表于: 2015-02-05   作者:conkeyn   来源:转载   浏览:
摘要: import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * json与java对象之间转化器
 * 
 * @version 1.0.0
 * @since 2014年10月21日 下午3:11:39
 */
public final class JsonObjectMapper extends ObjectMapper
{
	/** */
	private static final long serialVersionUID = 4563671462132723274L;

	public JsonObjectMapper()
	{
		super();
		//从JSON到java object
		//没有匹配的属性名称时不作失败处理
		this.configure(MapperFeature.AUTO_DETECT_FIELDS, true);

		//反序列化
		//禁止遇到空原始类型时抛出异常,用默认值代替。
		this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
		//禁止遇到未知(新)属性时报错,支持兼容扩展
		this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
		this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
		this.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
		this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);

		//序列化
		//禁止序列化空值
		this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
		this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
		this.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true);
		this.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
		this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true);
		//不包含空值属性
		this.setSerializationInclusion(Include.NON_EMPTY);
	}	

}

 

/**
 * 测试类
 * @version 1.0.0
 * @since 2015年2月5日 上午10:32:38
 */
public class User implements Serializable
{

	private String id;
	private String name;
	
	private int age;

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
}

 

@Test
	public void testJsonObjectMapperOutput() throws Exception
	{
		User user = new User();
		user.setAge(11);
		user.setId(UuidUtil.getUUID());
		JsonObjectMapper mapper = new JsonObjectMapper();
		String v = mapper.writeValueAsString(user);
		int idx = v.indexOf("\"name\":");
		Assert.assertEquals(idx, -1);
		System.out.println(v);
	}

 

#redis的服务器地址
redis.host=127.0.0.1
#redis的服务端口 
redis.port=6379
#密码
#redis.pass=1234xxxxx
#链接数据库
redis.default.db=0
#客户端超时时间单位是毫秒 
redis.timeout=100000
#最大连接数 
redis.pool.maxActive=300
#最大空闲数 
redis.pool.maxIdle=100
#最大建立连接等待时间  
redis.pool.maxWait=1000
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.pool.testOnBorrow=true
# 当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:websocket="http://www.springframework.org/schema/websocket"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/websocket
    http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">

	<!-- <bean id="propertyConfigurerRedis"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>classpath:config/redis-manager-config.properties</value>
			</list>
		</property>
	</bean> -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxActive}" />
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
		<property name="testOnReturn" value="${redis.pool.testOnReturn}" />

	</bean>
	<bean id="redisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="usePool" value="true" />
		<property name="hostName" value="${redis.host}" />
		<property name="port" value="${redis.port}" />
		<!--property name="password" value="${redis.pass}" / -->
		<property name="timeout" value="${redis.timeout}" />
		<property name="database" value="${redis.default.db}" />
		<constructor-arg index="0" ref="jedisPoolConfig" />
	</bean>
	
	
	<bean id="objectMapper" class="com.xx.xxx.core.utils.JsonObjectMapper" />

</beans>

 

<!-- jredis -->
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.6.2</version>
</dependency>
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>1.4.2.RELEASE</version>
</dependency>

 

JsonObjectMapper

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号