解决Spring中@ResponseBody 返回json字符串date类型变成long型的问题

阅读更多
package com.jynine.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
import org.springframework.stereotype.Component;
/**
 * 
 * 

Title: CustomObjectMapper.java

*

Description:解决SpringMVC使用@ResponseBody返回json时,日期格式默认显示为时间戳的问题

* @author jynine * @date 2014-11-14 */ @Component("customObjectMapper") public class CustomObjectMapper extends ObjectMapper{ public CustomObjectMapper() { CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date.class, new JsonSerializer() { @Override public void serialize(Date arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); arg1.writeString(sdf.format(arg0)); } }); this.setSerializerFactory(factory); } }


servlet-context.xml中添加


		
			
				
			
		
	

	
		
		
	

你可能感兴趣的:(spring,json,bean,java)