刚开始为拼车引擎编写我的 JSON 网络服务。我在尝试编写注册 API 时收到 HTTP 404 错误。
这就是我的问题所在
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration"
HTTP/1.1 405 Method Not Allowed
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration/Request"
HTTP/1.1 500 Internal Server Error
"http://localhost:8081/mCruiseOnCarPool4All/Registration/Request"
HTTP/1.1 404 Not Found
"http://localhost:8081/mCruiseOnCarPool4All/Registration"
HTTP/1.1 404 Not Found
我知道我在这里错过了一些非常愚蠢的东西。
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.mcruiseon.carpool4all</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/carpool4all/*</url-pattern>
RegistrationService.java,只编写了 Post 方法。我在所有异常上都返回错误,sessionkey
成功时就可以了。您可以忽略 post 方法中的代码,我只是想分享一下,以便您了解我的错误处理。
package com.mcruiseon.carpool4all;
@Path("/Registration")
public class RegistrationService {
private ClientSession clientSession ;
private String sessionKey ;
private SessionManager sessionManager ;
@Context
UriInfo uriInfo;
@Context
Request request;
@POST
@Path ("Request")
@Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<AMessageStrategy> element) {
try {
clientSession = new ClientSession(God.mCruiseOnServer) ;
} catch (InvalidServerDNSorIPException e) {
e.printStackTrace();
return Response.serverError().build() ;
}
sessionKey = sessionManager.setClientSession(clientSession) ;
clientSession.setSessionKey(sessionKey) ;
clientSession.getSendQueue().sendRequest(element.getValue()) ;
try {
clientSession.waitAndGetResponse(element.getValue()) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(sessionKey).build();
}
}
Junit 测试用例(去掉了所有的 HttpConnection 代码)
ClientIdentityConcrete clientIdentity = new ClientIdentityConcrete("username", "password", "secretkey") ;
RegistrationRequest register = new RegistrationRequest(clientIdentity);
String jsonStr = mapper.writeValueAsString(clientIdentity);
HttpPost request = new HttpPost("http://localhost:8081/mCruiseOnCarPool4All/Registration/Request");