泽西2.x和Tomcat8返回NullPointerException, HTTP状态500



我正试图为android应用程序做一个简单的休息服务,但我似乎无法让它工作。我得到404(如果我使用servlet 2.5和glassfish.servletcontainer)或null指针与http状态500和这个错误信息,如果我使用servlet 3.0。我想不出这个问题。

我想我把球衣1和球衣2混在一起了,但我不知道在哪里。

错误如下:

java.lang.NullPointerException
org.apache.catalina.loader.WebappClassLoaderBase.binaryNameToPath(WebappClassLoaderBase.java:2495)
org.apache.catalina.loader.WebappClassLoaderBase.findLoadedClass0(WebappClassLoaderBase.java:2711)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1184)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

我的web.xml是这样的:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
    <servlet-name>EndPoint</servlet-name>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>EndPoint</servlet-name>
    <url-pattern>/REST/*</url-pattern>
</servlet-mapping>

Pom dependencies:

<dependencies>
    <!-- Jax RS -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <!-- Jersey 2.23.2 -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey2.version}</version>
    </dependency>

. .简单的Java类:

@ApplicationPath("/EndPoint")
public class EndPoint extends Application{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMsg() {
       return Response.status(200).entity("hai!").build();
    }
}

将您的EndPoint类更改为:

@Path("/endpoint")
public class EndPoint{
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getMsg() {
   return Response.status(200).entity("hai!").build();
}
}

让我们假设EndPoint类是在com.test包,所以你需要改变web.xml配置为:

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>     
<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.test</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.xml中的<param-value>com.test</param-value>。它决定了Jersey应该在哪里搜索Rest类。

相关内容

  • 没有找到相关文章

最新更新