1

刚开始为拼车引擎编写我的 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");
4

1 回答 1

3

相关连接是/mCruiseOnCarPool4All/carpool4all/Registration/Request ,它返回 500 错误,因此您的服务器控制台上必须有错误堆栈跟踪。

您显示的其他 URL 命中 404 导致 URL 未指向您的 Jersey servlet,该 servlet 似乎已映射到/carpool4all

您的 URL 模式是:

 <host>/<app>/<jerseyservlet>/<xml resource>/<method path>

 - host = localhost:8081/ (obviously)
 - app = mCruiseOnCarPool4All
 - jerseyservlet = carpool4all
 - xml resource = Registration
 - method path = Request
于 2012-10-22T09:22:23.417 回答