Reputation: 15205
Quite a while ago, I had worked on a REST service that was implemented using Apache CXF. Earlier this year, I ported it to Jersey, but there is an incompatibility that I didn't notice at the time.
Apache CXF provides a convenient way to do "extension mapping". The clients of the original service were written to expect this. We're now discovering that using extensions in the client with the ported service isn't working, because extension mapping isn't configured in the service. After looking around for a while, I see that Jersey doesn't provide an obvious way to do this.
Short of changing the clients to not provide the extension, what are some possible strategies for "fixing" this in the service? I'm fairly certain that there are no clients that will be using XML.
Upvotes: 1
Views: 49
Reputation: 208944
Assuming you're using Jersey 2.x, there actually is a convenient way to handle this. It is with the ServerProperties.MEDIA_TYPE_MAPPINGS property. If you are using a ResourceConfig
subclass for your configuration you can just do
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("com.example");
Map<String, MediaType> mappings = new HashMap<>();
mappings.put("json", MediaType.APPLICATION_JSON_TYPE);
mappings.put("xml", MediaType.APPLICATION_XML_TYPE);
property(ServerProperties.MEDIA_TYPE_MAPPINGS, mappings);
}
}
If you are using a web.xml, you can do
<servlet>
<servlet-name>JerseyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.mediaTypeMappings</param-name>
<param-value>xml:application/xml, json:application/json</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Upvotes: 1