Jersey 2.6杰克逊提供者注册 [英] Jersey 2.6 Jackson provider registering
问题描述
我正在使用Jersey 2.6实现REST Web服务,
I'm implementing REST web service with Jersey 2.6,
我遇到了注册Jackson提供商以获得JSON支持的麻烦,我已根据jeresy实施文档( https://jersey.java.net/documentation/2.6/user-guide。 html#json.jackson )。
I'm having troubles of registering a Jackson Provider for JSON support, I have implemented according to the jeresy documentation (https://jersey.java.net/documentation/2.6/user-guide.html#json.jackson).
- 添加maven依赖 - jersey-media-json-jackson
- 实现了ContextResolver类。
- 使用@Provider对其进行注释以启用Auto-Discoverable Features
- web.xml具有提供者类的包名称,以便在扫描期间注册提供者。
- Add maven dependency - jersey-media-json-jackson
- Implemented a ContextResolver class.
- Annotated it with @Provider to enable "Auto-Discoverable Features"
- web.xml has the package name of the provider classes, so that Providers will be registered during the scan.
ref:
http://blog.dejavu.sk/2013/11/19/registering -resources-and-providers-in-jersey-2 /
出于某种原因Jackson JSON提供商未注册,我是missi某事?
For some reason Jackson JSON provider is not registered, am I missing something?
推荐答案
直到Jersey 2.9,该功能未被自动发现。我们需要(1)在 Application / ResourceConfig
子类中明确注册 JacksonFeature
,(2)列出Jackson在要扫描的包的web.xml中打包,或者(3)将JacksonFeature添加到web.xml中的提供者列表
Up until Jersey 2.9, that feature is not auto-discovered. We need to either (1) explicitly register the JacksonFeature
in the Application/ResourceConfig
subclass, (2) list the Jackson package in the web.xml of packages to scan, or (3) add the JacksonFeature to the list of providers in the web.xml
应用程序子类:
public class MyApplication extends ResourceConfig {
public MyApplication() {
// (1)
register(JacksonFeature.class); // <----- Jackson Support
packages("the.package.of.your.resources");
}
}
或web.xml:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<!-- (2) -->
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
the.package.of.your.resources,
org.codehaus.jackson.jaxrs <!-- Jackson providers -->
</param-value>
</init-param>
<init-param>
<!-- (3) -->
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.jackson.JacksonFeature
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
查看更多详情这里的第二期。请注意,对于 ... classnames
属性,如果要注册多个提供程序,则应将其列在以逗号,分号分隔的相同param-value中,或换行。
See more details here in the "Second Issue". Note that for the ...classnames
property, if you have more than one provider to register, you should list it in the same param-value delimited with a comma, semicolon, or newline.
哦,只是一个FYI, ContextResolver
只能注册 ObjectMapper
在可检索的上下文中,因此 MessageBodyReader / MessageBodyWriters
可以重用它。但它没有注册编组/解组所需的实际 MessageBodyReader / Writer
。
Oh and just an FYI, the ContextResolver
is only to register the ObjectMapper
in a retrievable context, so the MessageBodyReader/MessageBodyWriters
can reuse it. But it does not register the actual MessageBodyReader/Writer
that is required for the marshalling/unmarshalling.
这篇关于Jersey 2.6杰克逊提供者注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!