加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

rest – 找不到媒体类型= {application / xml,q = 1000}的Messag

发布时间:2020-12-16 23:29:37 所属栏目:百科 来源:网络整理
导读:我正在和Jersey一起编写一个RESTful Web服务.我想以 XML格式向消费者返回一个自定义对象.我得到的错误是: MessageBodyWriter not found for media type={application/xml,q=1000},type=class com.test.ws.Employee,genericType=class com.test.ws.Employee.
我正在和Jersey一起编写一个RESTful Web服务.我想以 XML格式向消费者返回一个自定义对象.我得到的错误是:

MessageBodyWriter not found for media type={application/xml,q=1000},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;
    }

}

以下是添加的库列表:

谁能帮我?

解决方法

我相信你的错误在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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读