在Jax-RS应用程序中注册JodaModule [英] Register JodaModule in Jax-RS Application
问题描述
我正在使用Jersey编写Jax-RS应用程序,并使用Jackson2来促进JSON i / o。服务本身工作正常,但我想通过让Jackson映射器自动将日期和日期时间序列化/反序列化为JodaTime对象来改进它。
I'm writing a Jax-RS application using Jersey, and Jackson2 under the hood to facilitate JSON i/o. The service itself works fine, but I'd like to improve it by having the Jackson mapper automagically serialize/deserialize date and date-times to JodaTime objects.
我是按照文档这里并添加了相关的罐子,但我在这个指令上丢失了:
I'm following the documentation here and have added the relevant jars, but I'm lost on this instruction:
Registering module
To use Joda datatypes with Jackson, you will first need to register the module first (same as with all Jackson datatype modules):
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
我试图在扩展jax.ws.rs.core的自定义类中执行此操作。应用程序,但我对该解决方案完全没有信心。我目前收到此错误:
I've tried to do this in the custom class that extends jax.ws.rs.core.Application, but I'm not at all confident in that solution. I'm currently getting this error:
Can not instantiate value of type [simple type, class org.joda.time.DateTime] from String value ('2014-10-22'); no single-String constructor/factory method
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3471b6d5; line: 7, column: 25]
除了这个模块注册需要在应用程序(servlet?)启动,我不知道如何处理这些信息。我是否需要使用特定的东西来注释自定义类以使其被拾取?我应该延长课程吗?
Other than the general impression that this module registration needs to happen at application (servlet?) startup, I have no idea what to do with this information. Do I need to annotate a custom class with something in particular to have it picked up ? Should I be extending some class ?
我在StackOverflow上找到的示例通常会将其粘贴在 main()
中并直接调用映射器,但是我我依赖杰克逊数据绑定,因此这些例子无关紧要。任何方向都很受欢迎。
The examples I find on StackOverflow usually stick it in main()
and call the mapper directly, but I'm relying on Jackson Databinding so the examples aren't relevant. Any direction is appreciated.
推荐答案
你基本上想要创建/配置/返回 ObjectMapper
在 ContextResolver
中。类似
You'll basically want to create/configure/return the ObjectMapper
in a ContextResolver
. Something like
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
final ObjectMapper mapper = new ObjectMapper();
public ObjectMapperContextResolver() {
mapper.registerModule(new JodaModule());
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
如果您使用包扫描来发现资源,那么 @Provider
注释应该允许发现和注册这个类。
If you are using package scanning to discover your resources, then the @Provider
annotation should allow this class to be discovered and registered also.
基本上会发生什么,是 MessageBodyReader
和 MessageBodyWriter
,分别用于解组和编组,将调用 ContextResolver
中的 getContext
方法,以确定 ObjectMapper
使用。读者/作者将在类中传入(在读者中它将是方法参数中预期的类型,在编写器中它将是返回为-a / in-a响应的类型),这意味着我们可以使用不同的方式为不同的类配置 ObjectMapper
,如此处。在上面的解决方案中,它用于所有类。
Basically what happens, is the the MessageBodyReader
and MessageBodyWriter
provided by Jackson, used for unmarshalling and marshalling, respectively, will call the getContext
method in the ContextResolver
, to determine the ObjectMapper
to use. The reader/writer will pass in the class (in a reader it will be the type expected in a method param, in a writer it will be the type returned as-a/in-a response), meaning we are allowed to use differently configured ObjectMapper
for different classes, as seen here. In the above solution, it is used for all classes.
这篇关于在Jax-RS应用程序中注册JodaModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!