强制glassfish 4使用杰克逊2.3

时间:2013-12-20 18:29:11

标签: glassfish jersey jackson jax-rs java-ee-7

我写了一个应该在Glassfish 4上运行的maven应用程序。

标准ApplicationConfig如下所示:

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

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        // following code can be used to customize Jersey 2.0 JSON provider:

        try {
           Class jsonProvider = Class.forName("org.glassfish.jersey.jackson.JacksonFeature");

        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        addRestResourceClasses(resources);
        return resources;
    }

现在的问题是,我生成Json的资源应该使用jackson 2.3注释。 但我的glassfish使用了一些codehaus。 ...包提供json。 codehaus是杰克逊的旧版本。我想使用来自fastxml的新的,它提供@JsonIdentityInfo注释。

我认为我可以写下来解决我的问题:

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

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();

        resources.add(JacksonFeatures.class);  //from the com.fasterxml.jackson.jaxrs.annotation Package
        resources.add(JacksonJaxbJsonProvider.class);
        resources.add(JacksonJsonProvider.class);

        addRestResourceClasses(resources);
        return resources;
    }

但没有效果。现在Glassfish使用标准的JsonProvider Moxy ...我不想使用它。你有什么提示我可以强迫glassfish使用我的库而不是buildin-libraries吗?或者我可以将buildin库更改为较新的库吗?


如果你知道如何解决这个问题。你能不能提供一些代码片段?


编辑1:


尝试第一种解决方法后:

new ApplicationConfig:

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

    public ApplicationConfig() {
        //register( new GZipEncoder() );
        register( JacksonFeature.class );
    }

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

}

JacksonFeature:

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

POM:

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.4</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.4</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
</dependencies>

依赖关系:

http://i.stack.imgur.com/uA4V2.png

错误:

SEVERE:   Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:325)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:177)
    ...

SEVERE:   Exception while loading the app
SEVERE:   Undeployment failed for context /blueserver
INFO:   file:/C:/bluetrail/blueserver/target/blueserver-0.0.0.1/WEB-INF/classes/_de.bluetrail_blueserver_war_0.0.0.1PU logout successful
SEVERE:   Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [IterableProvider<InjectionResolver<Object>>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 2 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.internal.inject.JerseyClassAnalyzer(@Named ClassAnalyzer, IterableProvider<InjectionResolver<Object>>)]

编辑2:

我现在将发布我项目的一些课程。在那些课程中,我包括了我使用的所有注释。

我喜欢这样的16个实体:

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@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:

import de.ibs.trail.entity.Address;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

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

最后我有很多像这样的资源:

import de.bluetrail.blueserver.dao.AddressDao;
import de.ibs.trail.entity.Address;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

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

}

这是第一部分。作为第二部分,我有一些Google服务课程。因为我想尝试使用一些Google GeoLocation和Routing。我把Google代码放到了pasteBin文件中,因为它太大了:

http://pastebin.com/u3e0dms6

我使用的库如下:

import com.fasterxml.jackson.databind.ObjectMapper;
import de.ibs.trail.entity.Address;
//some other entities
//...
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

我希望有所帮助。所有其他类使用相同的注释。

1 个答案:

答案 0 :(得分:10)

首先确保你的pom.xml中有以下内容:

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

然后确保在任何pom.xml中都没有这个:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.5</version>
</dependency>

然后你需要禁用moxy。最简单的方法是抛弃Application类并使用ResourceConfig类替换。首先让我们创建你的JacksonFeature课程:

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

这里有两个有趣的事情,首先我禁用了moxy,然后我确保添加JacksonException映射器。这样,如果存在解析或生成异常,您将获得比内部服务器错误更好的错误。好的,最后一步它将您的应用程序重写为ResourceConfig类:

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

    public RestApplication() {
        register( new GZipEncoder() );
        register( JacksonFeature.class );
    }

    private void addMyResources() {
        register( MyResource1.class );
        register( MyResource2.class );
    }
}

应该这样做。另外,不是像这样逐个注册资源,如果你知道它们的路径,你可以删除所有代码并将其添加到RestApplication的构造函数中:

package( "com.me.myrestresourcespackage" );

希望这有帮助。