Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON not working with CXF

Tags:

json

jackson

cxf

The JacksonJsonProvider is not working with CXF.

CXF v2.6.0 Jackson v2.1.2 (com.fasterxml.jackson) RESTClient (for testing)

I do have the provider configured like below in beans.xml.

<bean id="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
    <property name="dateFormat">
    <bean class="java.text.SimpleDateFormat">
    <constructor-arg type="java.lang.String" value="yyyy-MM-dd'T'HH:mm:ss.SSSZ">     </constructor-arg>
    </bean>
   </property>
</bean>

<bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider">
    <property name="mapper" ref="jacksonMapper" />
</bean>

in jaxrs:server.....>

<jaxrs:providers>
<ref bean="jaxbProvider" />
<ref bean="jacksonProvider" />                        
</jaxrs:providers>

</jaxrs:server>

The application gets deployed without any issues, it gives good JSON while I give the request as "application/xml" and the response as "application/json".

When I try to give JSON in request by setting Content-Type=application/json I'm facing the 500 Internal Server Error

The request is getting logged in the log file thru CXF-logging.

The request is not at all landing in the service implementation class of my webservice.

The JSON in request body is :

{"SearchOrdersRequest":{"LoginCredentials":{"AppId":"BookStore","Username":"myuser","Password":"abcd1234","SecurityToken":"Vcvx45YilzX1"},"SearchHeader":{"SearchCategory":"Rep","FilterLogic":"1 AND 2","SearchParams":{"Field":"Order Number (s)","Operator":"EQUALS","Values":"600045335"}}}} 

Any immediate help is appreciated.

like image 987
JBJ Avatar asked Jan 18 '13 13:01

JBJ


People also ask

Is Apache CXF a JAX-RS?

Overview. This tutorial introduces Apache CXF as a framework compliant with the JAX-RS standard, which defines support of the Java ecosystem for the REpresentational State Transfer (REST) architectural pattern.

What is Jackson JAX-RS JSON provider?

Description. com.fasterxml.jackson.jaxrs.json. Jackson-based JAX-RS provider that can automatically serialize and deserialize resources for JSON content type (MediaType). com.fasterxml.jackson.jaxrs.json.annotation. Package that contains annotations specific to JSON dataformat.

What is Jackson JAX-RS?

JAX-RS is a specification for creating REST web services in Java. JAX-RS requires an implementation such as Jersey, RESTEasy or Apache CXF. Jackson is a popular JSON parser for Java and can be integrated with JAX-RS using the jackson-jaxrs-providers multi-module project.

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.


2 Answers

In CXF documentation , you can see where you need to add json provider and include a dependency. But, I still getting errors when I tried to add jackson instead of jettison, after some hours I figured that you need to include one more jackson dependency.

  1. Add JSON provider

    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
    </jaxrs:providers>
    
  2. Add dependencies

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>1.9.12</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>1.9.12</version>
    </dependency>
    
like image 70
user1572786 Avatar answered Oct 06 '22 19:10

user1572786


As I undertood you, your application produces and consumes xml and json format. So, first of all. Make it sure that your cxf resource endpoint are able to do it.

@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

Otherwise your request won't find any resource implementation. (at these line at class level or method level)

Then if this is not enough check out this jackson cxf integration:

<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
<bean id="jsonContextResolver" class="net.sf.gazpachoquest.rest.support.JacksonContextResolver" />

Also

 <jaxrs:server id="services" address="/">
   <jaxrs:providers>
      <ref bean="jsonProvider" />
      <ref bean="jsonContextResolver" />
     </jaxrs:providers>
  </jaxrs:server>

The context resolver the class where the mapper is defined:

@Provider
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper = new ObjectMapper();

    public JacksonContextResolver() {
        /*
         * Register JodaModule to handle Joda DateTime Objects.
         * https://github.com/FasterXML/jackson-datatype-jsr310
         */
        mapper.registerModule(new JSR310Module());
        mapper.setSerializationInclusion(Include.NON_EMPTY);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    }

    @Override
    public ObjectMapper getContext(Class<?> arg0) {
        return mapper;
    }
}

And just in case you deploy your application into a j2ee container, you may require a application config class:

@ApplicationPath("/api")
public class ApplicationConfig extends javax.ws.rs.core.Application{

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        // add here your resources
        classes.add(JacksonContextResolver.class);
        classes.add(JacksonJsonProvider.class);
        ...
        return classes;
    }

Hope this help.

like image 40
Antonio Maria Sanchez Berrocal Avatar answered Oct 06 '22 18:10

Antonio Maria Sanchez Berrocal