我的web.xml是
<servlet>
<servlet-name>SwipeXBackendServices</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.mycompany.backend.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SwipeXBackendServices</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
为什么映射必须是/webservices/*
而不是com.mycompany.backend.webservices/*
。
这是我的网络服务代码
package com.swipex.backend.webservices;
@Path("/Activation")
public class Activation {
private static final Logger log = Logger.getLogger(Activation.class);
private DeviceDetails deviceDetailsTable = DeviceDetails.getInstance();
@POST
@Path("Request")
@Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<CDeviceDetails> device) {
编辑:Junit客户端代码
@Test
public void testPost() throws Exception {
/*
* Activate
*/
// Initialize Web Services
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
//
// Activation Service
//
URI url = UriBuilder.fromUri(
"http://" + SERVER + "/SwipeXBackEnd/Activation/Request")
.build();
WebResource service = client.resource(url);
System.out.println(url);
// Get the data ready
CDeviceDetails newDevice = new CDeviceDetails(null, "mac id", "model",
"manufacturer", "androidVersion", Calendar.getInstance()
.getTime(), "buildID", "fingerPrint", "board",
"bootLoader", "hardware", "id", "product", "serial", Calendar
.getInstance().getTime(), Calendar.getInstance()
.getTime(), Globals.ACTIVE);
String deviceUniqueIdentity = service.type(MediaType.APPLICATION_JSON)
.post(String.class, newDevice);
assertNotNull(deviceUniqueIdentity);
System.out.println("Activation Passed " + deviceUniqueIdentity);
答案 0 :(得分:1)
Url模式与您的java包结构没有任何关系。在您的情况下,您已映射 SwipeXBackendServices servlet以收听“ / webservices ”网址(例如:http://localhost:8080/webservices/
)。即,当'/ webservices'的请求出现时,它将被路由到您的'SwipeXBackendServices'servlet类。
此外,您的jax-rs(其余)课程“激活”也会映射到网址“ / activation ”(例如:http://localhost:8080/activation
)。
网址映射&amp; class:
/webservices ===> SwipeXBackendServices
/activation ===> Activation
因此,请勿将包名与网址名称混淆。
答案 1 :(得分:1)
正如Sriram所说,servlet映射与软件包无关。
com.sun.jersey.config.property.packages
servlet的SwipeXBackendServices
参数只是告诉Jersey要扫描哪个包来发现REST资源。
servlet-mapping
servlet的SwipeXBackendServices
告诉servlet容器servlet将回答哪些URL。在这种情况下,它将回答所有以/webservices
为前缀的网址。这意味着所有REST资源都将以/webservices
为前缀。
然后,任何REST资源的URL都将是
的串联因此,假设您的WAR的上下文根为/myWebApp
,则POST
资源的Activation
请求的网址为:http://localhost:8080/myWebApp/webservices/Activation/Request