刚开始为拼车引擎编写我的JSON Web服务。我尝试编写注册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");
答案 0 :(得分:3)
相关联系为/mCruiseOnCarPool4All/carpool4all/Registration/Request
并且它返回500错误,因此您必须在服务器控制台上有错误堆栈跟踪。
您正在显示的其他网址达到404,因为网址未指向您似乎映射到/carpool4all
的Jersey servlet
您的网址格式为:
<host>/<app>/<jerseyservlet>/<xml resource>/<method path>
带
- host = localhost:8081/ (obviously)
- app = mCruiseOnCarPool4All
- jerseyservlet = carpool4all
- xml resource = Registration
- method path = Request