Java休息Web服务与泽西异常

时间:2014-05-12 16:14:13

标签: java web-services rest jersey web.xml

我在java中有以下web服务。

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

}

以及以下web.xml文件。

    <?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>

我正在使用tomcat 7来运行它。我用泽西岛让事情发生。我在浏览器上运行时收到错误消息。 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)

你能帮我吗?我正在学习本教程http://java.dzone.com/articles/restful-web-services-java。与我的唯一不同的是,我使用端口8181,这是正确的,我使它与教程一起工作,但不是我当前的版本,我不知道为什么。

修改

经过一些更多的测试后,似乎最后两种方法是问题我认为它的路径设置方式。但我仍然无法弄清楚原因。

2 个答案:

答案 0 :(得分:0)

此问题与通过param name和param值初始化Jersey包有关。你的网址看起来很完美。

Param值应包含有效的包名称。

确保此程序包中存在LifeEvent类(autobiographical.aid.webservices)

这解决了这个问题。

答案 1 :(得分:-1)

问题是我在编辑中说过的特定网络服务中的路径集。

替换另一个没有解决问题的人解决了它。