سؤال

Hi i have the following webservice in java.

LifeEvent.java:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("LifeEventService")
public class LifeEvent {

    // @GET here defines, this method will method will process HTTP GET
    // requests.
    @GET
    // @Path here defines method level path. Identifies the URI path that a
    // resource class method will serve requests for.
    @Path("/place/{numPlaces}")
    // @Produces here defines the media type(s) that the methods
    // of a resource class can produce.
    @Produces(MediaType.TEXT_XML)
    // @PathParam injects the value of URI parameter that defined in @Path
    // expression, into the method.
    public String xPlaces(@PathParam("numPlaces") int numPlaces) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        String xmlFile = "";
        xmlFile = "<Place>" + "<Location>" + "<Name>" + "Campo Grande, Lisboa"
                + "</Name>" + "<Picture>" + "<Description>"
                + "Last day of school kakakaka" + "</Description>" + "<Image>"
                + "12182621051540x13" + "</Image>" + "</Picture>" + "<People>"
                + "<Name>" + "Fábio Santos" + "</Name>" + "<Relation>"
                + "Amigo" + "</Relation>" + "</People>" + "<People>" + "<Name>"
                + "José Carilho" + "</Name>" + "<Relation>" + "Amigo"
                + "</Relation>" + "</People>" + "</Location>" + "<Place>";
        return xmlFile;
    }

    @GET
    @Path("/place/{begin}/to/{end}")
    @Produces(MediaType.TEXT_XML)
    public String getPlaceToPlace(@PathParam("begin") int begin,
            @PathParam("end") int end) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        return "";
    }

    @GET
    @Path("/people/{type}")
    @Produces(MediaType.TEXT_XML)
    public String xPeople(@PathParam("type") String type) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        return "";
    }

    @GET
    @Path("/people/{begin}/to/{end}")
    @Produces(MediaType.TEXT_XML)
    public String getPeopleToPeople(@PathParam("begin") int begin,
            @PathParam("end") int end) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        return "";
    }

    @GET
    @Path("/people/{numPeople}/important")
    @Produces(MediaType.TEXT_XML)
    public String getPeopleByImportance(@PathParam("numPeople") int numPeople) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        return "";
    }

    @GET
    @Path("/people/{numPictures}/important")
    @Produces(MediaType.TEXT_XML)
    public String getPicturesByImportance(
            @PathParam("numPictures") int numPictures) {

        // String name = i;
        // return "<User>" + "<Name>" + /*name +*/ "</Name>" + "</User>";
        return "";
    }

}

and the following web.xml file.

    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>RememberMeServer</display-name> 
    <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>autobiographical.aid.webservices</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
    </web-app>

I am using tomcat 7 to run this. And i use Jersey to make stuff happen. I get a error message when i run this on the browser. http://localhost:8181/RememberMeServer/rest/LifeEventService/place/10

 Exception report

message Servlet.init() for servlet Jersey REST Service threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

Can you guys please help me. I was following this tutorial http://java.dzone.com/articles/restful-web-services-java. the only thing different from mine is that i use port 8181 and that is correct i made it work with the tutorial but not with my current version and i dont know why.

EDIT:

After some more tests it seems that the last two methods are the problem i think its the way the path is set. but i am still not able to figure out why.

هل كانت مفيدة؟

المحلول 2

The problem was the set of the path in that particular web service has i said in the edit.

Replacing it for another one that didn't had that problem solved it.

نصائح أخرى

This problem is related to the initialization of the Jersey packages through param name and param value. Your url looks perfect.

Param value should contain the valid package name.

Make sure the LifeEvent class exists in this package (autobiographical.aid.webservices)

That resolves the issue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top