使用Jersey中的配置属性
我使用java / jetty自托管服务器和jersey-2 for java RESTful api。 Application具有带有属性的application.properties文件。
ConfigurationProperties
类读取属性文件并将其加载到java.util.Properties
类中。
Jetty服务器实例化以下列方式完成。
// Create and register resources final ResourceConfig resourceConfig = new ApiServiceConfig() .register(new DependencyInjectionBinder()); ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); contextHandler.setContextPath("/mydomain/api"); Server jettyServer = new Server(8585); jettyServer.setHandler(contextHandler); ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(resourceConfig)); contextHandler.addServlet(jerseyServlet, "/*"); // Create web context. Can't use. //WebApplicationContext webContext = getWebApplicationContext(); // Add web context to servlet event listener. //contextHandler.addEventListener(new ContextLoaderListener(webContext)); try { jettyServer.start(); jettyServer.join(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { jettyServer.destroy(); }
我不能使用Spring AnnotationConfigWebApplicationContext
因为它需要commons-logging依赖,这在java-8中不起作用。
如何使用jetty / jersey上下文注册属性以及如何在以后检索值(例如: context.getProperty("prop.name")
)?
你可以…
只需将Properties
对象配置为注入,然后将其注入您需要的任何位置
final Properties props ... resourceConfig.register(new AbstractBinder() { @Override protected void configure() { bind(props).to(Properties.class); } }); @Path("config") public class ConfigResource { @Inject private Properties properties; }
你可以…
使用InjectionResolver
和自定义注释使单个属性可InjectionResolver
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public static @interface Config { String value(); } public class ConfigInjectionResolver implements InjectionResolver { private final Properties properties; public ConfigurationInjectionResolver(Properties properties) { this.properties = properties; } @Override public Object resolve(Injectee injectee, ServiceHandle> handle) { if (String.class == injectee.getRequiredType()) { Config annotation = injectee.getParent().getAnnotation(Config.class); if (annotation != null) { String prop = annotation.value(); return properties.getProperty(prop); } } return null; } ... } final Properties props... resourceConfig.register(new AbstractBinder(){ @Override protected void configure() { bind(new ConfigInjectResolver(props)) .to(new TypeLiteral>(){}); } });
然后将它与自定义注释一起使用
@Path("config") public class ConfigResource { @Config(PROP_KEY) private String propValue; @GET public String getConfigProp() { return propValue; } }
你可以…
使用我制作的小型图书馆
com.github.psamsotha jersey-properties 0.1.1 resourceConfig.register(JerseyPropertiesFeature.class); resourceConfig.property(JerseyPropertiesFeature.RESOURCE_PATH, "appication.properties"); @Path("test") public class SomeResource { @Prop("some.prop") private String someFieldProp; private String someConstructorProp; public SomeResource(@Prop("some.prop") String someConstructorProp) { this.someConstructorProp = someConstructorProp; } @GET public String get(@Prop("some.prop") String someParamProp) { return someParamProp; } }
你可以…
使用Spring。 我认为使用Java 8时遇到的问题是你使用的是Spring 3.x. 我认为Java 8不受支持。 使用带有java 8的Jersey / Spring4没有问题。如果你使用的是jersey-spring3依赖项,你需要排除spring3依赖项,并添加spring 4.请参阅这篇文章中的更新
也可以看看:
- 使用Jersey的配置属性