@ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter,它使用了Jackson 这个开源的第三方类库。主要是以下两个jar包:jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar。

@ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter,它使用了Jackson 这个开源的第三方类库。主要是以下两个jar包:jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar。

@ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter,它使用了Jackson 这个开源的第三方类库。主要是以下两个jar包:jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar。

出现问题: 使用@ResponseBody时返回json字符串的日期格式。Date类型属性默认返回一个Long型的时间戳,怎样能够返回自定义的日期格式?

解决方案:目前有两种方式实现,

1、局部修改(网上较多,但不推荐);

继承Jackson的抽象类:JsonSerializer<T>,然后在javabean的属性getter()上添加注解@JsonSerialize即可实现。

代码如下:

  1. import java.io.IOException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.codehaus.jackson.JsonGenerator;
  5. import org.codehaus.jackson.JsonProcessingException;
  6. import org.codehaus.jackson.map.JsonSerializer;
  7. import org.codehaus.jackson.map.SerializerProvider;
  8. /**
  9. * @description 自定义返回JSON 数据格中日期格式化处理
  10. * @author aokunsang
  11. * @date 2013-5-28
  12. */
  13. public class CustomDateSerializer extends JsonSerializer<Date> {
  14. @Override
  15. public void serialize(Date value,
  16. JsonGenerator jsonGenerator,
  17. SerializerProvider provider)
  18. throws IOException, JsonProcessingException {
  19. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20. jsonGenerator.writeString(sdf.format(value));
  21. }
  22. }

使用方式:

  1. @JsonSerialize(using = CustomDateSerializer.class)
  2. public Date getCreateDate() {
  3. return createDate;
  4. }

2、全局修改(强烈推荐):

MappingJacksonHttpMessageConverter主要通过ObjectMapper来实现返回json字符串。这里我们继承该类,注册一个JsonSerializer<T>。然后在配置文件中注入自定义的ObjectMapper。

代码如下:

  1. import java.io.IOException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.codehaus.jackson.JsonGenerator;
  5. import org.codehaus.jackson.JsonProcessingException;
  6. import org.codehaus.jackson.map.JsonSerializer;
  7. import org.codehaus.jackson.map.ObjectMapper;
  8. import org.codehaus.jackson.map.SerializerProvider;
  9. import org.codehaus.jackson.map.ser.CustomSerializerFactory;
  10. /**
  11. * @description 解决Date类型返回json格式为自定义格式
  12. * @author aokunsang
  13. * @date 2013-5-28
  14. */
  15. public class CustomObjectMapper extends ObjectMapper {
  16. public CustomObjectMapper(){
  17. CustomSerializerFactory factory = new CustomSerializerFactory();
  18. factory.addGenericMapping(Date.class, new JsonSerializer<Date>(){
  19. @Override
  20. public void serialize(Date value,
  21. JsonGenerator jsonGenerator,
  22. SerializerProvider provider)
  23. throws IOException, JsonProcessingException {
  24. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  25. jsonGenerator.writeString(sdf.format(value));
  26. }
  27. });
  28. this.setSerializerFactory(factory);
  29. }
  30. }

spring-servlet.xml中配置:

  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  4. <property name="objectMapper" ref="customObjectMapper"></property>
  5. </bean>
  6. </mvc:message-converters>
  7. </mvc:annotation-driven>
  8. <bean id="customObjectMapper" class="com.pmc.dwa.common.custom.CustomObjectMapper"></bean>

二、@DatetimeFormat使用注意

1、 使用@DatetimeFormat很简单,这里需要注意的是:使用时要引入一个类库joda-time-1.3.jar,否则会无法访问相应路径(400错误)。

ps:该注解可以作用在METHOD,FIELD以及PARAMETER级别上。

使用介绍参考:http://www.captaindebug.com/2011/08/using-spring-3-datetimeformat.html#.UaR3mWWZk0k

2、因为springMVC没有提供默认的日期转换器,前段页面传递过来日期字符串怎么转换为日期类型,如果没有提供全局日期转换器或者数据绑定的基础上,可以使用@DatetimeFormat注解完成。

本文转自http://blog.csdn.net/z69183787/article/details/40375831 感谢作者

@ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter,它使用了Jackson 这个开源的第三方类库。主要是以下两个jar包:jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar的更多相关文章

  1. java将JSON字符串转换为实体类对象,基于net.sf.json实现

    @SuppressWarnings("unchecked") public static <T> T jsonToObject(String jsonString, C ...

  2. JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分---https://blog.csdn.net/mathlpz126/article/details/80684034

    JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分 https://blog.csdn.net/mathlpz126/article/details/80684034

  3. 关于spring MVC 绑定json字符串与实体类绑定

    1 如果前台传json字符串,后台用@RequestBody 接收 前端 "content-Type":"application/json", 2  前台用fo ...

  4. Springmvc 中org.springframework.http.converter.json.MappingJackson2HttpMessageConverter依赖jackson包

    1,问题详情:Spring使用4.3.5.Release版本后 在SpringMvc配置文件中配置json 解析器后出现报错信息 [org.springframework.web.context.Co ...

  5. SpringMVC分页查询无法直接将对象转换成json的解决办法(报org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type:错)

    在用ajax获得分页数据时,无法将获取的值赋值给input标签,在修改用户信息时不显示用户已经注册的信息,百度可知 springmvc处理分页数据返回的对象时,无法直接将对象转换成json,会报org ...

  6. Spring 4.x org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ClassNotFoundException:

    Spring 4.x The first major version of Jackson is no longer supported in Spring 4. The class you want ...

  7. Not found org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    原因spring3跟spring4的jackson不一样. 解决方案: 1)spring3.x是org.springframework.http.converter.json.MappingJacks ...

  8. java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    原因是Spring 3.x 和4.X处理JSON的一个类不一样,而这个东西又配置在xml文件中,所以编译时又无法发现 spring3.x是org.springframework.http.conver ...

  9. Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]

    <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" cl ...

随机推荐

  1. 使用wkwebview后,页面返回不刷新的问题

    onpageshow 事件在用户浏览网页时触发. onpageshow 事件类似于 onload 事件,onload 事件在页面第一次加载时触发, onpageshow 事件在每次加载页面时触发,即 ...

  2. 238 Product of Array Except Self 除自身以外数组的乘积

    一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积.不用除法 且在O(n)内解决这个问 ...

  3. log4j2异步日志解读(一)AsyncAppender

    log4j.logback.log4j2 历史和关系,我们就在这里不展开讲了.直接上干货,log4j2突出于其他日志的优势,异步日志实现. 看一个东西,首先看官网文档 ,因为前面文章已经讲解了disr ...

  4. nodejs——避免判断创建多级目录

    基本概念 fs.exists已经弃用,可以使用fs.access判断文件夹是否存在,但是官方的建议是在进行文件操作前不要使用fs.access,官方推荐的方式的是直接进行文件操作,有错误再修改 不建议 ...

  5. 自学php【一】 任务:图片上传即时可见

    工作已经快2周了,头儿给派了个任务做个企业站!这几天正在紧锣密鼓的作战中!等忙完了这个活!写下自己的学习心得体会!与看到文章的您一起分享! 在这里记录每次遇到的难题,如何解决的! 今天要做的功能就是实 ...

  6. jsTree插件简介(三)

    UI-plugin JSTree的UI插件:用来处理选择.不选和鼠标悬浮树选项的插件. 一.属性包括: 1.select_limit:指定一次可以选中几个节点,默认为-1,表示无限制选中. 2.sel ...

  7. HDU_1227_Fast Food_动态规划

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1227 Fast Food Time Limit: 2000/1000 MS (Java/Others)   ...

  8. HDU_1175_连连看

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大意:连连看规则,只能转两次弯,先输入矩阵0表示没有棋子,正整数表示不同的棋子,然后询问,输入两点坐 ...

  9. layer实现窗口抖动效果

    function showMsg(msg, icon){ layer.msg(msg, { //1:正确:2:错误:3:询问:4:锁定:5:失败:6:成功:7:警告:16:加载 icon : icon ...

  10. vue中axios设置

    //设置默认全局baseURL axios.defaults.baseURL=process.env.BASE_API; //设置默认全局携带浏览器cookie axios.defaults.with ...