Sunday, July 8, 2012

Bootstrapping Jersey With POJO / JSON Mapping

Jersey is the JAX-RS reference implementation by Sun / Oracle. The bootstrapping process is relatively straightforward. It is done by declaring Servlet(s) to handle the Jersey requests.

The basic configuration (without POJO / JSON mapping) can be found here: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

To add POJO / JSON mapping:
  • Ensure the following jar files are on classpath:
    • asm
    • jackson-core-asl
    • jackson-jaxrs
    • jackson-mapper-asl
    • jackson-xc
    • jersey-core
    • jersey-json
    • jersey-servlet
    • jersey-server
  • web.xml should look something like this:

<servlet>
<servlet-name>WsServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.xxxxx.ws</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>WsServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

No comments:

Post a Comment