How do I configure the date format the server returns using CXF JAX-RS and Jackson 2 in XML?

血红的双手。 提交于 2019-12-25 04:11:04

问题


This took me a lot of effort to figure out so I'm going to answer the question below. This answer doesn't use annotations and does not require creating additional classes.


回答1:


You put this in your spring xml context configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

...

<bean id="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
    <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg type="java.lang.String" value="yyyyMMdd'T'HHmmss.SSSZ"/>
        </bean>
    </property>
</bean>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"
      p:mapper-ref="jacksonMapper"/>

...

     <jaxrs:providers>
        <ref bean="jsonProvider"></ref>
     </jaxrs:providers>
  </jaxrs:server>


来源:https://stackoverflow.com/questions/28508917/how-do-i-configure-the-date-format-the-server-returns-using-cxf-jax-rs-and-jacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!