简体   繁体   中英

Newbie Java EE, Json webservice getting a HTTP 404, using Jersey SDK

Just started to write my JSON webservices for a carpool engine. I am getting a HTTP 404 error as I try to write my registration API's.

This is where my problem is

"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

I know I am missing something really silly here.

Web.xml (Jersey Library)

<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, only coded the Post method. I am returning errors on all exceptions and ok with sessionkey when successful. You can ignore the code in the post method, I just wanted to share so that you understand my error handling.

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 test case (removed all the HttpConnection code)

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");

The relevant connection is /mCruiseOnCarPool4All/carpool4all/Registration/Request and it return a 500 error so you must have an error stacktrace on your server console.

The other URLs that you're showing are hitting 404 cause the URLs are not pointing to your Jersey servlet which seemed to be mapped to /carpool4all

Your URL pattern is :

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

with

 - host = localhost:8081/ (obviously)
 - app = mCruiseOnCarPool4All
 - jerseyservlet = carpool4all
 - xml resource = Registration
 - method path = Request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM