servlet [Jersey REST Service] 的 Servlet.service() java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27098445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:12:29  来源:igfitidea点击:

Servlet.service() for servlet [Jersey REST Service] java.lang.NullPointerException

javaservletsjersey-2.0

提问by blitzen12

can anyone help me work around with this one. This is my first time to create restful api.

谁能帮我解决这个问题。这是我第一次创建restful api。

web.xml

网页.xml

 <display-name>1FxAll</display-name>
<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.sample.blitz</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

UserController.java

用户控制器.java

package com.sample.blitz;

@Path("/user")
public class UserController{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String test(){
        return "user";
    }
}

full stacktrace error.

完整的堆栈跟踪错误。

SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/1FxAll] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.sample.blitz.UserController.test(UserController.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:151)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:384)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:342)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
    at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:271)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

it keep saying that the error is at UserController line 28 but when I check there is no line 28.

它一直说错误在 UserController 第 28 行,但是当我检查时没有第 28 行。

I am using Tomcat 7.0. I also tried to switch to glassfish but I got the same error.

我正在使用 Tomcat 7.0。我也尝试切换到 glassfish,但我遇到了同样的错误。

Thanks..

谢谢..

回答by blitzen12

I'll just post my answer here.

我只会在这里发布我的答案。

I'm not sure what's relevant with glassfish 4.0and 4.1but after I change glassfish version from 4.0to 4.1and remove jersey file from lib folder it seems working but in tomcat I still got NullPointerException. This will only work on glassfish 4.1.

我不知道什么是与相关的glassfish 4.04.1,但之后我改变从GlassFish的版本4.0,以4.1和删除文件的球衣从lib文件夹似乎工作,但在Tomcat中我仍然有NullPointerException异常。这仅适用于glassfish 4.1.

回答by Naveenkumar

You have add the following dependency for the RESTFull

您已经为 RESTFull 添加了以下依赖项

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
</dependency>

回答by OO7

Remove /from @Path("/user")& make it as @Path("user"). Bcoz, u have already given path like /api/*in the <url-pattern>I think there is double occurrences of /

删除/@Path("/user")&使它作为@Path("user")。Bcoz,你已经给出了路径/api/*<url-pattern>我认为有两次出现/

Change your UserControllerClass as

将您的UserController班级更改为

package com.sample.blitz;

@Path("user")
public class UserController{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String test(){
        return "user";
    }
}

回答by Noli

When you create object of the service instantiate it with new key word.

当您创建服务的对象时,用新的关键字实例化它。

Xservice x = new Xservice();