1

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

web.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

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$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.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$1.run(ServerRuntime.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.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.

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

Thanks..

blitzen12
  • 1,350
  • 3
  • 24
  • 33
  • I think there is a call to `/1FxAll` path not `/user` ? How did u give a call to `UserController's test()` ? – OO7 Nov 24 '14 at 05:44
  • This is the url I used. http://localhost:8080/1FxAll/api/user – blitzen12 Nov 24 '14 at 05:52
  • Is `/1FxAll` is the display name of your application or something else ? – OO7 Nov 24 '14 at 06:04
  • Yes it's the display name. I edited my question. I added the display name. – blitzen12 Nov 24 '14 at 06:07
  • R u running this app on Tomcat under Eclipse ? Did u try to run it outside of Eclipse environment ? I am not sure but, check whether libraries r correct ? If u r using Maven then check for `artifactId`. – OO7 Nov 24 '14 at 06:25
  • Yes I am running it in eclipse. I am not using maven. – blitzen12 Nov 24 '14 at 06:39
  • Try to run this outside of Eclipse & check whether u added correct version of libraries. Bcoz, it's unable to scan the package for ur resources. Check whether u get same error from this. If u r using Servlet 3.0 then try configurations given [Deployment of a JAX-RS application using web.xml with Servlet 3.0](https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3) – OO7 Nov 24 '14 at 06:58
  • 1
    Ok remove `/` from `@Path("/user")` & make it as `@Path("user")` & let's try. Bcoz, u have already given path like `/api/*` in the *url pattern* I think there is double occurrences of `/`. – OO7 Nov 24 '14 at 07:09
  • Thanks. I got it working now. I got the same error on glassfish 4.0 and tomcat 7.0 but I got it working with glassfish 4.1. I will just upvote your comment. Thank you. – blitzen12 Nov 24 '14 at 07:13
  • Did u try this change & run it on Tomcat 7 &/or GlassFish 4.0 ? Did u made any change to ur `UserController` class to make it working on GlassFish 4.1 ? *If my comment is helpful to u then I'll make it as answer so that others will get solution to same problem*. – OO7 Nov 24 '14 at 07:19

4 Answers4

1

I'll just post my answer here.

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

blitzen12
  • 1,350
  • 3
  • 24
  • 33
0

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 /

Change your UserController Class as

package com.sample.blitz;

@Path("user")
public class UserController{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String test(){
        return "user";
    }
}
OO7
  • 2,785
  • 1
  • 21
  • 33
  • 3
    This sounds unlikely. [Jersey specification](https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e1721) says: `A @Path value may or may not begin with a '/', it makes no difference. Likewise, by default, a @Path value may or may not end in a '/', it makes no difference, and thus request URLs that end or do not end in a '/' will both be matched`. – botchniaque Nov 24 '14 at 09:03
  • this is wrong, it doesn't matter to user or don't use the `/` – William Kinaan Sep 21 '15 at 14:48
0

You have add the following dependency for the 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>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Naveenkumar
  • 483
  • 5
  • 20
-1

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

Xservice x = new Xservice();
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Noli
  • 1