找不到媒体类型= {application / xml,q = 1000}的MessageBodyWriter - Jersey + Jaxb

时间:2015-05-24 10:17:18

标签: rest jaxb jersey jax-rs

我正在为Jersey编写一个RESTful Web服务。我想以XML格式向消费者返回一个自定义对象。我得到的错误是:

  

找不到媒体类型= {application / xml,q = 1000}的MessageBodyWriter,type = class com.test.ws.Employee,genericType = class com.test.ws.Employee。

以下是代码:

的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> 

服务类

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;
    }

}

以下是添加的库列表:

Jars

任何人都可以帮助我吗?

5 个答案:

答案 0 :(得分:2)

我相信你的错误在web.xml中。尝试在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>

答案 1 :(得分:1)

解决问题的一种方法是创建自定义javax.ws.rs.core.Applicationorg.glassfish.jersey.server.ResourceConfig。您的服务器似乎没有检测到序列化的提供程序。通过实施您自己的Application,您将能够指定要使用的提供商。举个例子,你可以做的是:

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);
    }
}

在部署文件中添加应用程序:

的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>

答案 2 :(得分:0)

Employee类应该实现Serializable接口

答案 3 :(得分:-1)

我能够自己解决这个问题。这是因为构建路径中包含冲突的jar。这是jar文件的快照。

enter image description here

答案 4 :(得分:-1)

我管理一个需要添加REST Web服务的旧项目。这个没有Maven。

对于球衣2.25(最后一次使用Java SDK 1.7编译),我解决了添加jar的问题

jersey-media-jaxb-2.25.jar