我试图创建一个简单的Restful Web Service,但是当我尝试调试它时,“回头客”如图所示发生了错误。我可以知道是什么原因吗?返回包含数据的客户Customer时,似乎无法使用JAXB将Java对象转换为XML。
package com.mkyong.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/xml/customer")
public class XMLService {
@GET
@Path("/{pin}")
@Produces(MediaType.APPLICATION_XML)
public Customer getCustomerInXML(@PathParam("pin") int pin) {
Customer customer = new Customer();
customer.setName("mkyong");
customer.setPin(pin);
return customer;
}
}
package com.mkyong.rest;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"pin"
})
@XmlRootElement(name = "customer")
public class Customer {
String name;
int pin;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
}
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“> 宁静的Web应用程序
<servlet>
<servlet-name>JAVA API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
我在此示例中发现了错误,最新的Eclipse IDE已弃用xml jar,您必须在运行和调试时(在配置中)在IDE中手动将其重新添加–