简体   繁体   中英

Java RS null pointer exception when switching from Intellij to Maven build

I had some code working when building using Intellij. I switched to Maven because I wanted an easily executable jar file, but now the service isn't behaving the same way.

I'm expecting to be able to do curl -X POST http://localhost:8080/api/values --data "value=10" and receive it here:

@Path("api/values")
public class Receive {
    @EJB
    Tracker tracker;

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Response doPost(@FormParam("value") int val) {
        Logger l = Logger.getLogger("org.glassfish.grizzly.http.server.HttpHandler");
        l.setLevel(Level.FINE);
        l.setUseParentHandlers(false);
        ConsoleHandler ch = new ConsoleHandler();
        ch.setLevel(Level.ALL);
        l.addHandler(ch);
        l.log(Level.FINE, val);
        tracker.increaseSum(10);
        return Response.status(200).entity("ok").build();
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }

but I'm now getting a null pointer exception:

Apr 30, 2017 5:35:07 PM org.glassfish.grizzly.http.server.HttpHandler$1 run
FINE: service exception
java.lang.NullPointerException
    at com.underdog.jersey.grizzly.Receive.doPost(Receive.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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:164)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:158)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
    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:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:381)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:219)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:748)

Am I missing a dependency in my pom or something? The simple @GET method seems to be working fine.

EDIT: Changes made:

@Path("/api/values")
public class Receive {

    @Inject
    public Tracker tracker;

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public String doPost(@FormParam("value") int val) {
        this.tracker.increaseSum(val);
        return "\nok\n";
    }

    // Tell the tracker to set sum == 0, effectively "erasing" the "stored"
    // values.
    @DELETE
    @Produces(MediaType.TEXT_PLAIN)
    public Response deleteSum() {
        this.tracker.clearSum();
        return Response.status(200).entity("\nok\n").build();
    }
}

@Path("/api/sum/values")
public class Send {
    @Inject
    public Tracker tracker;

    @GET
    @Produces("text/plain")
    function
}

@Immediate
public class Tracker {
    ...
    @PostConstruct
    function 

    @PreDestroy
    function

    @Lock(LockType.READ)
    function
}

//Defines the base URI for all resource URIs.
@ApplicationPath("/api")
public class AppConfig extends ResourceConfig {

    public AppConfig() {
        packages(true, "com.underdog.jersey.grizzly");
        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(Tracker.class)
                    .to(Tracker.class)
                    .in(Immediate.class);
            }
        });
    }
}

Error after mvn clean package

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project jersey-grizzly: Compilation failure: Compilation failure:
[ERROR] /home/user/kochava/simple-service/src/main/java/com/underdog/jersey/grizzly/Tracker.java:[18,7] error: duplicate class: Tracker
[ERROR] /home/user/kochava/simple-service/src/main/java/com/underdog/jersey/grizzly/Send.java:[16,11] error: cannot access Tracker
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

If you're not running an an EE container, then EJB will not work. You should get rid of any EE dependencies in your project, so you are not tempted to use anything that will not work.

Instead, you should just use Jersey internal DI system HK2 to handle injection of services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM