在JAX-RS响应中使用ISO-8601日期 [英] Use ISO-8601 dates in JAX-RS responses
问题描述
我使用GlassFish 4上的Java EE 7构建RESTful Web服务。在序列化包含 java.util.Date
对象的POJO时,不包括时区信息。
I'm building RESTful web services using Java EE 7 on GlassFish 4. When serializing POJOs containing java.util.Date
objects, the timezone information is not included.
如何自定义对象序列化,以便 java.util.Date
s包含时区信息?
How can I customize object serialization so java.util.Date
s have timezone info included?
例如,不是这样:
For example, instead of this:
{
"id": 1234,
"foo": "2014-05-19T13:53:49.392"
}
我反而喜欢这样:
{
"id": 1234,
"foo": "2014-05-19T13:53:49.392+09:30"
}
其中服务器的时区为GMT + 09:30。
Where the server's timezone is GMT + 09:30.
推荐答案
这是由bug ,这是GlassFish中JAX-RS的默认JSON(de)序列化程序。
This is caused by a bug in MOXy, which is the default JSON (de)serializer for JAX-RS in GlassFish.
解决方法是将JSON提供商切换到Jackson。
The workaround is to switch JSON providers to Jackson.
第一步是将这些依赖添加到构建中:
$ b
The first step is to add these dependencies to the build:
<!-- Provides JacksonFeature -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.8</version>
<scope>provided</scope>
</dependency>
<!-- Provides ObjectMapper and SerializationConfig -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
<type>jar</type>
</dependency>
下一步,添加 JacksonFeature
作为提供者,它将取代MOXy作为JSON序列化程序:
Next step, add JacksonFeature
as a provider, which will replace MOXy as the JSON serializer:
@ApplicationPath("/api/1")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
// Add Jackson feature.
resources.add(org.glassfish.jersey.jackson.JacksonFeature.class);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
// JAX-RS resource classes added here - maintained by NetBeans.
}
}
Jackson的默认行为是表示 java.util.Date
自时代形式以来的毫秒数。要将其更改为ISO-8601:
$ b
The default behaviour of Jackson is to represent java.util.Date
in its milliseconds since the epoch form. To change that to ISO-8601:
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
@Provider
@Produces("application/json")
public class JacksonConfigurator implements ContextResolver<ObjectMapper> {
public JacksonConfigurator() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
mapper.setDateFormat(dateFormat);
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> clazz) {
return mapper;
}
private final ObjectMapper mapper = new ObjectMapper();
}
这篇关于在JAX-RS响应中使用ISO-8601日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!