1

I use Jersey and Jackson to implement RESTful services provided by my server. Data is exchanged between the client and server as JSON documents. Jackson does the mapping between the JSON documents and the POJOs. This works well.

But I ran into one issue. When calling a service with a malformed JSON document, the server returns with an 500 error. I would expect a 400 BAD-Request error instead. Some searching in the Jersey docs showed me that ExceptionMappers can be used to archive this behavior. I also found out that Jackson already has a implementation a JsonParseExceptionMapper but it never gets called.

Do I have to register the mapper and if so how can I do this outside of the source code.?

Charles
  • 48,924
  • 13
  • 96
  • 136
Flo
  • 26,717
  • 14
  • 82
  • 124
  • see thread: http://stackoverflow.com/questions/3293599/jax-rs-using-exception-mappers – Alex Stybaev Aug 07 '12 at 09:25
  • Yes, I saw this question, but I don't want to implement the custom mapper class but use the existing one instead. So I'm looking for a way to register it outside of the source code. – Flo Aug 07 '12 at 09:35

1 Answers1

2

Ok I found out how to register the mapper classes.

In your web.xml where Jersery ServletContainer is registered you have to pass the Jackson package name org.codehaus.jackson.jaxrs beside your package name e.g. com.example.myapp.api;. The server then scans these packages on start up and registers the listener it find.

<servlet>
        <servlet-name>Jersey</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.example.myapp.api;org.codehaus.jackson.jaxrs</param-value>
        </init-param>
</servlet>
Flo
  • 26,717
  • 14
  • 82
  • 124