从Intellij切换到Maven构建时的Java RS空指针异常

时间:2017-05-01 00:43:49

标签: java maven jersey grizzly java-ws

在使用Intellij构建时,我有一些代码正常工作。我切换到Maven因为我想要一个易于执行的jar文件,但现在该服务的行为方式不同。

我期待能够做到 curl -X POST http://localhost:8080/api/values --data "value=10"并在此处收到:

@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!";
    }

但我现在得到一个空指针异常:

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)

我错过了我的pom中的依赖吗?简单的@GET方法似乎工作正常。

编辑:所做的更改:

@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);
            }
        });
    }
}

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

1 个答案:

答案 0 :(得分:1)

如果您没有运行EE容器,那么EJB将无法运行。您应该摆脱项目中的任何EE依赖项,因此您不会想要使用任何无效的东西。

相反,您应该使用Jersey内部DI系统HK2来处理服务注入。