我有 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 的 jersey 时,如何设置媒体类型映射?