Java 使用 RESTEasy 3.x 更改默认 JSON 时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19229341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Default JSON Time Format with RESTEasy 3.x
提问by insaneshow
I am using RESTEasy to implement a REST Service using JSON serialization. Currently, Dates are getting serialized to milliseconds since 1970. To improve compatibility, I would like to get my dates into one of two formats; milliseconds + timezone offset or ISO 8061.
我正在使用 RESTEasy 来实现使用 JSON 序列化的 REST 服务。目前,自 1970 年以来,日期被序列化为毫秒。为了提高兼容性,我想将日期转换为两种格式之一;毫秒 + 时区偏移或 ISO 8061。
It seems that RESTEasy used to use Jettison for JSON serialization, but from what I've been reading they've switch to Hymanson ... all of this has made googling for help pretty hit or miss.
似乎 RESTEasy 曾经使用 Jettison 进行 JSON 序列化,但从我读过的内容来看,他们已经转向 Hymanson ......所有这些都使得谷歌搜索的帮助非常受欢迎。
From what I can tell, I need to implement a ContextResolver along the lines of:
据我所知,我需要按照以下方式实现 ContextResolver:
public class HymansonConfig impelments ContextResolver<ObjectMapper>
{
private final OBjectMapper objectMapper;
public HymansonConfig() throws Exception
{
objectMapper = new ObjectMapper.configure(
SerializationFeature.WRITE_DATE_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> arg0)
{
return objectMapper;
}
}
The thing I haven't been able to find, is what do I do with this? Where do I put it?
我一直找不到的东西,我该怎么办?我把它放在哪里?
So the larger questions are, am I heading in the right direction and are my assumptions correct?
所以更大的问题是,我是否朝着正确的方向前进,我的假设是否正确?
采纳答案by gregwhitaker
You need to register your ContextResolver
implementation with Resteasy. You can do this by annotating your class with the @Provider
annotation and allowing Resteasy to automatically scan it during startup, registering it in web.xml, or registering it in a class that extends javax.ws.rs.core.Application
(if that is how you are bootstrapping Resteasy).
您需要向ContextResolver
Resteasy注册您的实现。您可以通过使用注释对您的类进行@Provider
注释并允许 Resteasy 在启动期间自动扫描它、在 web.xml 中注册它或在扩展类中注册它来实现这一点javax.ws.rs.core.Application
(如果这是您引导 Resteasy 的方式)。
Registering via Annotations
通过注解注册
@Provider
public class HymansonConfig implements ContextResolver<ObjectMapper>
{
private final ObjectMapper objectMapper;
public HymansonConfig() throws Exception
{
objectMapper = new ObjectMapper.configure(
SerializationFeature.WRITE_DATE_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> arg0)
{
return objectMapper;
}
}
Verify that classpath scanning is enabled in your web.xml file like so:
验证您的 web.xml 文件中是否启用了类路径扫描,如下所示:
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
NOTE:If you are deploying this in JBoss 7 do not set the resteasy.scan
context parameter as it is enabled by default.
注意:如果您在 JBoss 7 中部署它,请不要设置resteasy.scan
上下文参数,因为它默认启用。
Registering via web.xml
通过 web.xml 注册
Add the following context parameter to your web.xml
file. The value of the parameter should be the fully qualified class name of your ContextResolver
.
将以下上下文参数添加到您的web.xml
文件中。参数的值应该是你的ContextResolver
.
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>foo.contextresolver.HymansonConfig</paramvalue>
</context-param>
Registering via Application
通过应用程序注册
If you are using an Application class to configure Resteasy you can add your provider to the set of services and providers to register with Resteasy like so:
如果您使用 Application 类来配置 Resteasy,您可以将您的提供者添加到服务和提供者的集合中以向 Resteasy 注册,如下所示:
public class MyApp extends Application
{
@Override
public Set<Class<?>> getClasses()
{
HashSet<Class<?>> set = new HashSet<Class<?>>(2);
set.add(HymansonConfig.class);
set.add(MyService.class);
return set;
}
}
回答by Fabio De Carli
Using with the JSR310 (new api date) - LocalDate, LocalDateTime, LocalTime
与 JSR310(新 api 日期)一起使用 - LocalDate、LocalDateTime、LocalTime
Add dependence:
添加依赖:
<dependency>
<groupId>com.fasterxml.Hymanson.datatype</groupId>
<artifactId>Hymanson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
And create a provider to register the module:
并创建一个提供程序来注册模块:
@Provider
public class HymansonConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public HymansonConfig() throws Exception {
objectMapper = new ObjectMapper()
.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS )
.disable( SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
.setSerializationInclusion( JsonInclude.Include.NON_NULL )
.registerModule( new JSR310Module() );
}
@Override
public ObjectMapper getContext( Class<?> arg0 ) {
return objectMapper;
} }
回答by Radoslav Ivanov
Just annotate your fields with (note the string literal could be externalized/referred from a constant):
只需注释您的字段(注意字符串文字可以从常量外部化/引用):
@javax.json.bind.annotation.JsonbDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
Date myDate;