myrequest
POST http://someIPA:PORT/MyWARFILENAME/backend/Activation/Request
我正在为REST api调用获得404。这是我第一次尝试运行它。我知道我在这里遗漏了一些非常基本的东西。
Web.xml
<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.mycompany.backend.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
服务代码
package com.mycompany.backend.webservices;
@Path("/Activation")
public class Activation {
@POST
@Path("Request")
@Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<CDeviceDetails> data) {
// Do my thing, set everything_is_ok to true/false
if (everything_is_ok)
return Response.ok(everthing).build();
else
return Response.notModified().build();
}
}
junit测试代码
//
// Activation Service
//
URI url = UriBuilder.fromUri(
"http://" + SERVER + "/MyWARFILENAME/backend/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);
服务器端日志/var/log/tomcat7/localhost_access_log.2014-09-28.txt
IPA - - [TIMESTAMP]&#34; POST / MyWARFILENAME / backend / Activation / Request HTTP / 1.1&#34; 404 1049
这表示请求已到达服务器。即使在 //做我的事情...... 也有例外,它应该没关系,我不应该得到404
。
我怀疑web.xml上有些可疑。
答案 0 :(得分:0)
我通过从webservices
到backend
的web.xml映射进行了修改,并开始工作。
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/backend/*</url-pattern>
</servlet-mapping>