gpt4 book ai didi

java - 在 Jersey 中使用配置属性

转载 作者:行者123 更新时间:2023-12-01 07:21:18 26 4
gpt4 key购买 nike

我使用 java/jetty 自托管服务器和 jersey-2 作为 java RESTful api。应用程序具有带有属性的 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,因为它需要公共(public)日志记录依赖项,而这在 java-8 中不起作用。

如何使用 jetty/jersey 上下文注册属性以及稍后如何检索值(例如:context.getProperty("prop.name"))?

最佳答案

你可以...

只需将 Properties 对象配置为可注入(inject)对象,并将其注入(inject)到您需要的任何地方

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 和自定义注释使各个属性可注入(inject)

@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

另请参阅:

关于java - 在 Jersey 中使用配置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com