0

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
FutureShocked
  • 779
  • 1
  • 10
  • 26

1 Answers1

1

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.

Graham
  • 7,431
  • 18
  • 59
  • 84
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • So I tried to work through that documentation. I created a class extending `ResourceConfig`, registered an `AbstractBinder` that `configure`s by binding `Tracker` to `Tracker` in `Immediate` (as Tracker has some stuff it needs to do as the service starts and is destroyed) and replaced `@Singleton` with `@Immediate` as the annotation for the class. I have two classes trying to `@Inject` tracker. Now compilation errors that `Tracker` is a duplicate class and `Send`, the other class accesses it, cannot access it. Any other suggestions? – FutureShocked May 02 '17 at 21:08
  • Yeah. Post what you did as an edit to your post and also post the complete error. Can't help without it. What you're saying doesn't make much sense to me – Paul Samsotha May 03 '17 at 01:06
  • Alright, made the edit. Let me know if that makes more sense, I'm 100% new to this stuff. Also, thanks for being so helpful. – FutureShocked May 03 '17 at 02:35
  • I'm not sure what that means. I've never seen it. It's a compile error, so its something you're not doing correctly in your coding. Not sure what it is. – Paul Samsotha May 03 '17 at 03:42
  • Did you add the import? Do you have two of the same class defined? Try cleaning to e project t. `mvn clean` – Paul Samsotha May 03 '17 at 03:43
  • Oh yeah, turns out that `Tracker` didn't have package information yet. It looks like I'm down to run time errors. https://pastebin.com/dw3heF0R I get that this is way beyond the scope of my original question, so no hard feelings if you're getting sick of helping me out. – FutureShocked May 03 '17 at 04:16
  • 1
    You need to enable immediate scope. See [this post](http://stackoverflow.com/a/29292733/2587435) – Paul Samsotha May 03 '17 at 04:38
  • Well that at least removed runtime errors. Still not seeing the functionality I had in Intellij, but that is likely just a bug on my part. Just to clarify though, `ImmediateFeature` is standalone class to get a handle on the service locator, or `Tracker` should implement `Feature`? – FutureShocked May 03 '17 at 05:03
  • Do it just like in the post. Remember what I said, don't expect any EE functionality if you are not in an EE container. – Paul Samsotha May 03 '17 at 05:07