我正在尝试使用 jersey webservice 在休息时发送 json 对象。为此,我正在尝试一个简单的示例。但我收到内部服务器错误。我没有 Maven 项目。这是我的代码。我添加了罐子。
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>DataShareWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.abc.CustomerInfo.WebService</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>
客户.java
package abc;
public class Customer {
private int custNo;
private String custName;
private String custCountry;
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustCountry() {
return custCountry;
}
public void setCustCountry(String custCountry) {
this.custCountry = custCountry;
}
}
CustWebService.java
package abc
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("/CustWebService")
public class CustWebService {
private static final Logger LOGGER = LoggerFactory
.getLogger(ConfigureAndrew.class);
@GET
@Path("/{cusNo}")
@Produces(MediaType.APPLICATION_JSON)
public Customer produceCustomerDetailsinJSON(@PathParam("cusNo") int no) {
Customer cust = new Customer();
cust .setCustNo(no);
cust .setCustName("Java4s");
cust .setCustCountry("India");
return cust;
}
我的 Web-INF-->lib 文件夹中有以下 jar 1)asm-3.3.1.jar 2)jersey-bundle-1.14.jar 3)jersey-client-1.17.jar 4)json-simple-1.1.1 .jar 5)logback-access-1.0.9.jar 6)logback-classic-1.0.13.jar 7)logback-core-1.0.13.jar 8)servlet-api.jar 9)slf4j-api-1.7。 2.jar 10)slf4j-ext-1.6.1.jar
我在构建路径中添加了以下 jar: 1)jersey-json-1.8 2)jackson-core-asl-1.9.2.jar 3)jackson-mapper-asl-1.9.2.jar 4)jersey-media-json -jackson-2.3.1.jar