[英]Using guice, how do I configure Jersey's media type mappings?
我已經讓Guice配置了Jersey,而且效果很好。 到目前為止,這是我正在做的事情:
import com.google.common.collect.ImmutableMap;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletScopes;
import com.company.app.ejb.lookup.EjbProvider;
import com.company.app.rest.exception.ConditionExceptionMapper;
import com.company.app.rest.exception.InvalidSortExceptionMapper;
import com.company.app.rest.exception.NotFoundExceptionMapper;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
/*
From http://randomizedsort.blogspot.com/2011/05/using-guice-ified-jersey-in-embedded.html
*/
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
install(new EjbProvider());
install(new JacksonJsonProviderProvider());
// Must configure at least one JAX-RS resource or the
// server will fail to start.
bind(RestApiVersion1.class).in(ServletScopes.REQUEST);
bind(RestApiVersion2.class).in(ServletScopes.REQUEST);
bind(ConditionExceptionMapper.class).in(Singleton.class);
bind(InvalidSortExceptionMapper.class).in(Singleton.class);
bind(NotFoundExceptionMapper.class).in(Singleton.class);
// bind(ResourceConfigClass.class).in(Singleton.class); this does not work :(
// Route all requests through GuiceContainer
serve("/*").with(GuiceContainer.class, ImmutableMap.of(JSONConfiguration.FEATURE_POJO_MAPPING, "true"));
}
});
}
}
我有一個非常簡單的ResourceConfigClass,它看起來像這樣:
import com.sun.jersey.api.core.PackagesResourceConfig;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
public class ResourceConfigClass extends PackagesResourceConfig {
public ResourceConfigClass(String... packages) { //this constructor needs to be here, do not delete it or else the com.sun.jersey.config.property.packages param can't be passed in.
super(packages);
}
public ResourceConfigClass(Map<String, Object> props) { //this constructor needs to be here, do not delete it or else the com.sun.jersey.config.property.packages param can't be passed in.
super(props);
}
@Override
public Map<String, MediaType> getMediaTypeMappings() {
Map<String, MediaType> map = new HashMap<String, MediaType>();
map.put("xml", MediaType.APPLICATION_XML_TYPE);
map.put("json", MediaType.APPLICATION_JSON_TYPE);
return map;
}
}
但是,我不知道如何配置Guice以使用我已經完成的ResourceConfigClass。 如果bind
它,則在啟動時會出現此錯誤:
#|2014-06-04T10:11:03.324-0700|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=345;_ThreadName=Thread-2;|Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: com.google.inject.CreationException: Guice creation errors:
1) Could not find a suitable constructor in com.company.app.rest.resource.ResourceConfigClass. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.company.app.rest.resource.ResourceConfigClass.class(ResourceConfigClass.java:13)
at com.company.app.rest.resource.GuiceServletConfig$1.configureServlets(GuiceServletConfig.java:38)
在使用Guice之前,可以通過將其放在web.xml中來使用此類:
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.company.app.rest.resource.ResourceConfigClass</param-value>
</init-param>
我不知道如何在GuiceServletConfig
設置該屬性。 當我將球衣與guice一起使用時,如何設置媒體類型映射?
我能夠這樣做:
serve("/*").with(GuiceContainer.class, ImmutableMap.of(
JSONConfiguration.FEATURE_POJO_MAPPING, "true",
"com.sun.jersey.config.property.packages", "com.fasterxml.jackson.jaxrs.json",
"com.sun.jersey.config.property.resourceConfigClass", "com.company.app.rest.resource.ResourceConfigClass"
));
但是我發現了一種更直接的方法:
serve("/*").with(GuiceContainer.class, ImmutableMap.of(
JSONConfiguration.FEATURE_POJO_MAPPING, "true",
ResourceConfig.PROPERTY_MEDIA_TYPE_MAPPINGS, "xml:" + MediaType.APPLICATION_XML_TYPE + ",json:" + MediaType.APPLICATION_JSON_TYPE
));
現在,我不需要額外的ResourceConfigClass
類。 此格式記錄在com.sun.jersey.api.core.ResourceConfig#PROPERTY_MEDIA_TYPE_MAPPINGS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.