Ani*_*ket 5 rest jax-rs jaxb jersey
我正在和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>
Run Code Online (Sandbox Code Playgroud)
服务类
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是添加的库列表:
谁能帮我?
| 归档时间: |
|
| 查看次数: |
10753 次 |
| 最近记录: |