- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我已阅读 Jersey documentation ,并表示 Jersey 在读取实体后自动关闭连接(例如 response.readEntity(SomeObject.class)) 但是当抛出异常
Jersey vs Jersey (Standalone) vs Jersey with Grizzly vs Jersey with Tomcat - for REST services 有什么区别
如何通过 guice 使用非 Jersey 资源和 Jersey 资源? 我希望“/”由普通 servlet 处理。但我希望 Jersey 处理“/users”。 假设我有一个带@Path("/use
我正在尝试使用 Maven、Apache Tomcat 7.0、Eclipse IDE 创建一个基本的 RESTful 应用程序。我在 google 提供的一些示例代码中遇到了 jersey-serv
我已经从 Jersey 1.7 升级到 2.16,但 Jersey 似乎无法找到我的资源(请参阅下面的堆栈)。任何想法发生了什么?我尝试在扩展 ResourceConfig 的自定义应用程序类中初始化
我正在使用 com.yammer.dropwizard.config.Environment addProvider 方法在 Jersey 中注册提供程序。我也有一个自定义提供程序,它执行类似于 Dr
在 Jersey 1.x 中,您可以使用 ContainerRequest.getFormParameters()对表单数据进行请求过滤,但我在 Jersey 2.x 中看不到明显的等价物。我已经实现
我正在使用Jersey的集成Jackson处理将传入的JSON转换为POJO,例如: @POST @Consumes(MediaType.APPLICATION_JSON) public Respon
我正在尝试以编程方式创建 Jersey 资源(没有注释)。我有一个将 Name 和 id 作为输入参数的方法 raiseAlarm。我想从 JSON 输入中获取名称,并且我希望 id 来自路径参数。代
Dropwizard official documentation Jersey 客户端不可测试,有人有 dropwizard Jersey 客户端样本吗? 最佳答案 我发现在 Dropwizard
我一直在寻找解决这个问题的方法,但没有成功。我发现的最新帖子可以追溯到 2010 年。我正在使用带有嵌入式 grizzly 2.2.1 的 Jersey 1.12。 如果我理解正确,除非我将 Jers
我想开发一个 Web API,它将生成和使用 JSON 和 XML 数据。 我已经使用JAXB来支持XML,并且工作正常。现在我想添加 JSON 类型。我研究了不同的教程,所有教程都使用不同的依赖项,
如此处所述:http://wikis.sun.com/display/Jersey/WADL 我在 Tomcat 6 中使用 Jersey 1.4。 我已经尝试了所有可能的带有“/applicatio
我是jax-rs的新手,并且已经用jersey和glassfish构建了Web服务。 我需要的是一种方法,服务启动后即被称为。在这种方法中,我想加载自定义配置文件,设置一些属性,编写日志等等。 我尝试
当客户端请求 Not Acceptable MIME 类型时,如何防止 Jersey 在客户端发送 HTML 页面?我想使用 ExceptionMapper,但我不确定要捕获什么异常,或者这是否是处理
我试图在它的 JSON 被解码后拦截一个资源调用。通过阅读一些论坛和帖子,我发现我可以通过实现 来做到这一点。 org.glassfish.jersey.server.spi.internal.Res
我的 webapp 包含一个库,其中包含一个用 @javax.ws.rs.ext.Provider 注释的类。 .如果存在此类,我的 web 应用程序(在 EAR 中部署为 WAR)将无法启动并显示以
我想自定义404响应,即服务器(不是我)在找不到请求的资源时抛出(或自己抛出一个自定义的WebApplicationException,如果可以测试一个应用程序中是否存在请求的资源)?资源列表存储在某
我有一个受 Shibboleth(SSO 实现)保护的 Jersey API。 Shibboleth 将登录用户的 ID 放入请求属性中。在后端,我使用 Shiro 进行授权。 Shiro 希望了解登
我目前正在使用 Jersey 返回 JSON。我该如何返回 JSONP?例如我当前的RESTful方法是: @GET @Produces(MediaType.APPLICATION_JSON) pub
我是一名优秀的程序员,十分优秀!