Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson fails to detect JAXB annotation even after adding the Object Mapper

Tags:

json

jaxb

jackson

I am trying to configure a simple example for REST service using CXF + Jackson with JaxB @XmlRootElement annotation. I have already gone through various similar questions in stack overflow as well as blogs like this and have come up with the below solution:

Created a custom object mapper as follows:

public class CustomObjectMapper extends ObjectMapper{

    public CustomObjectMapper() {
    super();
    super.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    super.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
    AnnotationIntrospector secondary =  new JacksonAnnotationIntrospector();
    AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
    super.setAnnotationIntrospector(pair);
    }

}

Passed this to Json provided's constructor in spring config

  <bean id="jacksonMapper" class="com.myorg.test.CustomObjectMapper" />

    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
            <constructor-arg ref="jacksonMapper" />
        </bean>
    </jaxrs:providers>

Added a simple class JsonBean with JAXB Root element annotation

@XmlRootElement(name = "JsonBean")
public class JsonBean {
    private String val1;
    private String val2;

    public String getVal1() {
        return val1;
    }

    public void setVal1(String val1) {
        this.val1 = val1;
    }

    public String getVal2() {
        return val2;
    }

    public void setVal2(String val2) {
        this.val2 = val2;
    }

}

When I pass the below json,

{
    "JsonBean": {
        "val1": "Hello",
        "val2": "Hi"
    }
}

it's throwing below exception:

WARNING: javax.ws.rs.InternalServerErrorException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "JsonBean" (Class com.cognizan
t.poc.connectedcar.JsonBean), not marked as ignorable
 at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@1f2fae4; line: 2, column: 13] (through reference chain: com.myorg.test.Json
Bean["JsonBean"])
        at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:243)
        at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:99)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:163)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
        at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
        at org.eclipse.jetty.server.Server.handle(Server.java:368)
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
        at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:953)

Am I missing something here? Please help.

like image 494
Gireesh Avatar asked Apr 09 '13 16:04

Gireesh


1 Answers

I got it right.

I missed to add unwrap for de-serialization in the custom mapper:

added the below line in CustomObjectMaller

super.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

also, modified the spring config file as:

<jaxrs:providers>
    <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider">
       <property name="mapper" ref="jacksonMapper" />
   </bean>
</jaxrs:providers>

It started working as expected..

like image 64
Gireesh Avatar answered Nov 25 '22 00:11

Gireesh