0

我有下一个休息服务:

@ApplicationPath("geo")
@Path("weather")
public class MainResource extends Application {

    @Inject
    private MainDep dep;

    @GET
    public String printGotIt() {
        return "Got it!";
    }

    @GET
    @Path("propaganda")
    public String printPropaganda() {
        return dep.printPropaganda();
    }
}

MainDep 依赖代码:

public class MainDep {
    public String printPropaganda() {
        return "Interesting enterprise";
    }
}

当我尝试在以下 url 上使用资源时:host:port/root/geo/weather GlassFish 抛出了 javax.servlet.ServletException:

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
`javax.servlet.ServletException: Servlet.init() for servlet com.app.weather.rs.MainResource threw exception
root cause`
A MultiException has 1 exceptions.  They are:
    1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MainDep,parent=MainResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,22064320)
    root cause
    org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MainDep,parent=MainResource,qualifiers={}),position=-1,optional=false
4

2 回答 2

1

您需要添加@RequestScoped到您的班级。

于 2013-11-10T01:56:09.567 回答
1

这里的问题是您将 JAX-RS 应用程序 + JAX-RS 资源类混合在一个类中,最重要的是您将 CDI 注入添加到混合中。

尝试将 JAX-RS 应用程序与 JAX-RS 资源分开,例如:

@ApplicationPath("geo")
public class MainApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MainResource.class);
        return classes;
    }
}

@Path("weather")
public class MainResource {

    @Inject
    private MainDep dep;

    @GET
    public String printGotIt() {
        return "Got it!";
    }

    @GET
    @Path("propaganda")
    public String printPropaganda() {
        return dep.printPropaganda();
    }
}
于 2013-11-11T20:56:11.287 回答