Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBodyWriter not found for media type={application/xml, q=1000} - Jersey + Jaxb

I am writing a RESTful web service with Jersey. I want to return a custom object in XML form to consumer. The error I am getting is:

MessageBodyWriter not found for media type={application/xml, q=1000}, type=class com.test.ws.Employee, genericType=class com.test.ws.Employee.

Below is the code:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.ws</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app> 

Service Class

package com.test.ws;

@Path("/hello")
public class Hello {

    @GET 
    @Path("/sayHello")
    @Produces(MediaType.APPLICATION_XML)
    public Employee sayHello() {
        Employee employee = new Employee();
        employee.setEmpId(1);
        employee.setFirstName("Aniket");
        employee.setLastName("Khadke");
        return employee;
    }
}

Employee.java

package com.test.ws;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
public class Employee {

    public String firstName;

    public String lastName;
    public int empId;

    public Employee(String firstName, String lastName, int empId) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.empId = empId;
    }

    public Employee() {
        super();
    }

    @XmlElement
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlElement
    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

}

And here is the list of libraries added:

Jars

Can anyone help me?

like image 648
Aniket Avatar asked May 24 '15 10:05

Aniket


People also ask

What is a JAX-RS messagebodywriter?

A JAX-RS MessageBodyWriter is responsible for converting Java types to a stream. In this example we are creating a JAX-RS MessageBodyWriter that produces text/html. This class may be annotated using the @Produces annotation to restrict the media types for which it will be considered suitable.

How do I get JSON response from a web service?

JSON Response in Web Service. To change a web service that is already written to provide a XML response or to create a new one, we need to add just two things. Use @Produces annotation and add the MIME type application/json. Add dependent JAR files to produce JSON response.

Which RESTful web service is capable of producing XML response?

Same RESTful web service is capable of producing XML response as we have added XML MIME type also in @Produces annotation.


2 Answers

I believe your error is in the web.xml. Try changing your part to this in your web.xml.

<servlet>
       <servlet-name>Jersey REST Service</servlet-name>
       <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <!-- Register resources and providers under com.vogella.jersey.first package. -->
       <init-param>
           <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.test.ws</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
</servlet>
like image 119
Jelte Avatar answered Oct 18 '22 02:10

Jelte


One way to solve your issue is to create a custom javax.ws.rs.core.Application or org.glassfish.jersey.server.ResourceConfig. It seems that your server does not detect your providers for the serialization. By implementing your own Application, you will be able to specify which provider you want to use. for your example, what you could have done is :

MyApplication.java

package com.test.ws;

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        //register your resources
        packages("com.test.ws");
        //if you're using Jackson as your XMLProvider for example
        register(JacksonJaxbXMLProvider.class);
    }
}

And add the application in your deployment file :

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <display-name>com.vogella.jersey.first</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.test.ws.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>
like image 41
DragonCai Avatar answered Oct 18 '22 04:10

DragonCai