简体   繁体   English

如何使用XML的CXF JAX-RS和Jackson 2配置服务器返回的日期格式?

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

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. 该答案不使用注释,并且不需要创建其他类。

You put this in your spring xml context configuration: 您将其放在spring xml上下文配置中:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM