我使用java / jetty自托管服务器和jersey-2 for java RESTful api。 Application具有带有属性的application.properties文件。
java.util.Properties
类读取属性文件并将其加载到 // 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();
}
类中。
Jetty服务器实例化以下列方式完成。
AnnotationConfigWebApplicationContext
我不能使用Spring context.getProperty("prop.name")
,因为它需要commons-logging依赖,这在java-8中不起作用。
如何使用jetty / jersey上下文注册属性以及如何在以后检索值(例如:@Api(value = "CRM Services", description = "CRM Operations")
)?
答案 0 :(得分:5)
只需将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
和自定义注释
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public static @interface Config {
String value();
}
public class ConfigInjectionResolver implements InjectionResolver<Config> {
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<InjectionResolver<Config>>(){});
}
});
然后将其与自定义注释一起使用
@Path("config")
public class ConfigResource {
@Config(PROP_KEY)
private String propValue;
@GET
public String getConfigProp() {
return propValue;
}
}
使用我制作的small library
<dependency>
<groupId>com.github.psamsotha</groupId>
<artifactId>jersey-properties</artifactId>
<version>0.1.1</version>
<dependency>
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.请参阅the UPDATE in this post
另见: