【问题标题】:CDI HK2 Error in JaxRS ApplicationJaxRS 应用程序中的 CDI HK2 错误
【发布时间】:2014-01-12 11:31:57
【问题描述】:

我为 Glassfish 4 编写了一个 JaxRS 应用程序。它使用 Jackson 2。我可以毫无问题地构建它,但是当我部署它时,我遇到以下 4 个错误中的一个或多个。

Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [IterableProvider<InjectionResolver<Object>>]

和/或

org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001414 Bean name is ambiguous. Name JerseyClassAnalyzer resolves to beans [Managed Bean ...

和/或

org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [MultivaluedParameterExtractorProvider] ...

和/或

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>]

据我所知,当 glassfish 尝试加载一个类两次时可能会引发最后一个异常?

我上传了我的直接和间接依赖项的屏幕截图。 http://i.stack.imgur.com/HEtb1.png

关于其他解决方案,我尝试将&lt;scope&gt;provided&lt;/scope&gt; 添加到包含这些类的包中。 --> 没有成功

你有什么想法吗?

编辑 1:

我的资源配置:

@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        register( JacksonFeature.class );
    }

    private void addMyResources() {
        //a lot of resources.
    }
}

启用 Jackson 2:

public class JacksonFeature implements Feature {

    public boolean configure( final FeatureContext context ) {

        String postfix = '.' + context.getConfiguration().getRuntimeType().name().toLowerCase();

        context.property( CommonProperties.MOXY_JSON_FEATURE_DISABLE + postfix, true );

        context.register( JsonParseExceptionMapper.class );
        context.register( JsonMappingExceptionMapper.class );
        context.register( JacksonJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class );

        return true;
    }
}

很多这样的实体:

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "id", scope=Address.class)
//the JsonIdentityInfo is the reason i need Jackson 2
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String postalCode;
    private String city;
    private String country;
    private String street;
    private String houseNumber;
    @Embedded
    private Coordinate coordinate;
    //getters, setters , etc.
}

然后我有很多这样的 DAO:

@Stateless
public class AddressDao extends GenericDao {
    public Address getAddress(long id){
        return em.find(Address.class, id);
    }

    public List<Address> getAddresses(){
        List<Address> address = em.createQuery("SELECT a FROM Address a", Address.class).getResultList();
        return address;
    }
}

还有很多类似的资源:

@Path("dummy")
@Stateless
public class DummyResource {

    @Context
    private UriInfo context;
    @Inject userAuth user;
    @Inject addressDao AddressDao;

    public DummyResource() {
    }

    @GET
    @Produces("application/json")
    public List<Address> getAddress() {
        return AddressDao.getAddresses();
    }

}

编辑 2:

我创建了一个新的测试项目。一旦我向 org.glassfish.jersey.server 添加依赖项,我就会得到错误。

编辑 3:

我做了一个错误的测试应用程序:

http://www.file-upload.net/download-8459084/testApplication_20131229.rar.html

【问题讨论】:

  • 我将其部署为战争

标签: cdi glassfish-4 java-ee-7 weld hk2


【解决方案1】:

如果您没有在应用程序中使用 CDI,您可以在部署应用程序时执行此操作:

asadmin deploy --property implicitCdiEnabled=false

这将关闭 CDI 在部署应用程序时执行的隐式类扫描。更多关于 CDI 默认使用的信息可以在这里找到:https://blogs.oracle.com/theaquarium/entry/default_cdi_enablement_in_java

如果您使用 CDI,您可能想尝试将 beans.xml 添加到应用程序中的 Jersey jar 中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="none">
</beans>

【讨论】:

  • 我正在使用 CDI,我需要它。
  • jar 是只读的。还是你的意思是我的申请?当我将它添加到我的应用程序时,它不会改变任何东西。
  • 我想我需要准确了解您正在部署的内容。你是部署耳朵还是战争?在任何一种情况下,您可能都需要使用上面的 beans.xml 将 jersey 重新打包到您自己的耳朵中,以便关闭该组件的 CDI。说实话,Jersey 应该为这个问题提供开箱即用的 beans.xml,你可能应该用他们打开一个 Jira
  • 好的,我将在我的初始请求中描述我在我的应用程序中所做的事情。
  • 我需要使用 org.glassfish.jersey.server.ResourceConfig 和 org.glassfish.jersey.CommonProperties。我应该把 beans.xml 放在这两个中吗?
猜你喜欢
  • 1970-01-01
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多