2

我为 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

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

你有什么主意吗?

编辑1:

我的资源配置:

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

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

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

启用杰克逊 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

4

1 回答 1

1

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

asadmin 部署 --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>
于 2013-12-24T15:15:48.120 回答