package com.webservice.rest.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="employee")
public class Employee {
String name;
String id;
String location;
float salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Name [Id=" + id + ", Location=" + location
+ ", Salary=" + salary + "]";
}
}
这是我的employeewebservice
package com.webservice.rest.jaxb;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/employee")
public class EmployeeResource {
@GET
@Path("/getjson")
@Produces({MediaType.APPLICATION_JSON})
public Employee getJSon() {
Employee emp = new Employee();
emp.setName("Ankesh");
emp.setId("2096");
emp.setLocation("Pune_Kolkata");
emp.setSalary(2097868);
return emp;
}
}
下面是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>JaxbWithrs</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.webservice.rest.jaxb</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</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>
Iam得到以下例外
找不到媒体类型= application / json的MessageBodyWriter, type = class com.webservice.rest.jaxb.Employee,genericType = class com.webservice.rest.jaxb.Employee。
下面是我的lib目录的截图
如果我错过任何事情,请告诉我!