我有一个简单的webapp与jersey 2.6,jboss 5.0.0配合得很好。 将所有注入类(服务)声明为AbstractBinder的方法并不优雅。
所以我想使用CDI注入来管理所有生命周期bean管理。我已经看到使用@manageBean代替@ScopeResquest可能。我尝试了这个错误:
> javax.servlet.ServletException: A MultiException has 3 exceptions.
> They are:
> 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at
> Injectee(requiredType=ServiceExemple,parent=RestService,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,2072559159)
> 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of fr.xx.xx.rest.RestService errors were
> found
> 3. java.lang.IllegalStateException: Unable to perform operation: resolve on fr.xx.xx.rest.RestService
>
> org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
> org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
我的配置pom:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.6</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</exclusion>
<exclusion>
<artifactId>jaxb-impl</artifactId>
<groupId>com.sun.xml.bind</groupId>
</exclusion>
<exclusion>
<artifactId>jettison</artifactId>
<groupId>org.codehaus.jettison</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.1.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>${jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<version>2.25</version>
</dependency>
</dependencies>
<build>
<finalName>RESTfulExample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
我的web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- <init-param> -->
<!-- <param-name>jersey.config.server.provider.packages</param-name> -->
<!-- <param-value>fr.xx.xx.rest</param-value> -->
<!-- </init-param> -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>fr.xx.xx.ApplicationRest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.beanValidation.disable.validateOnExecutableCheck.server</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我的班级申请路径:
@ApplicationPath("rest")
public class ApplicationRest extends ResourceConfig {
public ApplicationRest() {
// Register resources and providers using package-scanning.
packages("fr.xx.xx.rest");
packages("org.codehaus.jackson.jaxrs");
register( JacksonFeature.class );
register(org.glassfish.jersey.server.validation.ValidationFeature.class);
// register(new ApplicationBinder()); => don't want use it
我的简单服务类:
@Default
public class ServiceExemple {
...
并完成我的资源课程:
@Path("/service")
@ManagedBean
public class RestService {
@Inject private ServiceExemple service;
...
我在WEB-INF
中添加了一个空bean.xml感谢您的帮助
答案 0 :(得分:0)
首先注入的资源(bean)和Jersey Endpoint类(注入点)必须是CDI-Aware。必须由CDI检测。我们可以使用bean-discovery-mode =“all” - 然后CDI扫描所有类或 bean-discovery-mode =“annotated”并使用PROPER注释标记我们的类:从这里:Bean defining annotations。我更喜欢@Dependent或@RequestScoped
然后我们必须使用Jersey Extension
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x-servlet</artifactId>
<version>{version}</version>
<scope>runtime</scope>
</dependency>
`
将CDI与HK2发现机制连接起来。 这是Official oracle Guideline