无法使泽西岛映射正常工作

时间:2014-06-01 05:27:16

标签: java rest maven tomcat jersey

我是Java的新手,我基本上刚开始使用PHP来自Java。

我试图在Jersey和Tomcat的帮助下启动RESTful API并运行。但我似乎犯了一个错误,我找不到任何与我得到的错误信息相同的帖子或帖子。我非常感谢有关此问题的任何帮助,谢谢!

这是我的Maven依赖项

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-servlet</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-simple-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-servlet</artifactId>
        <version>2.9</version>
    </dependency>
</dependencies>

这是web.xml

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

这是我的代码

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

@Path("/account")
public class AccountREST
{

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String info(){
        return "This is the RESTful API";
    }
    @GET
    @Path("/getit")
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt(){
        System.out.println("Got it!");
        return "Got it!";
    }
}

当我进入网址http://localhost:8080/rest/account/getit时,我收到此错误消息,我无法解读

java.lang.NullPointerException
    org.apache.catalina.loader.WebappClassLoader.binaryNameToPath(WebappClassLoader.java:2496)
    org.apache.catalina.loader.WebappClassLoader.findLoadedClass0(WebappClassLoader.java:2702)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1223)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1175)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2451)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2440)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

我最终得到了它的工作,我现在已经挣扎了好几个小时了。您在上面发布的文章与我的问题有些相关,但在其中一个Jersey API中。我没有正确实施泽西岛。我找到了所有的指南和教程,只是部分解释了如何实现Jersey RESTful API,这让我很困惑。

对于遇到与我相同问题的人来说,这是我的新设置:

<强>的pom.xml

<dependencies>
    <!-- Servlet 3.0 API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- JAX-RS RESTful Web Services -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.9</version>
    </dependency>
</dependencies>

<强>的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

</web-app>

<强> RESTConfig.class

import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/rest")
public class RESTConfig extends Application
{

    public RESTConfig(){}

    @Override
    public Set<Class<?>> getClasses( )
    {
        final Set<Class<?>> returnValue = new HashSet<Class<?>>( );
        returnValue.add( Account.class );
        return returnValue;
    }
}

<强> Account.class

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

@Path("/account")
public class Account
{

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String info(){
        return "This is the Account RESTful API";
    }

    @GET
    @Path("/getit")
    @Produces(MediaType.TEXT_PLAIN)
    public String getit(){
        System.out.println("Got it!");
        return "Got it!";
    }

}