如何在过滤器模式下使用 java @Configuration Spring 到 运行 球衣进行配置
How to configure with java @Configuration Spring to run jersey in filter mode
嗨,我在 application.properties 中有条目:
spring.jersey.type=filter
这允许我在过滤模式下 运行 带有 spring 的球衣,多亏了它,我可以共享 API 资源和静态内容,例如主页或 swagger 文档。
我知道,为了这项服务,我将始终 运行 这件球衣。所以它永远不会改变。我想使用 java 来配置它,比如 f.e。:这里:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
// scan the resources package for our resources
packages(getClass().getPackage().getName() + ".resources");
property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
}
是否可以使用 java 代码表示此 属性 条目:spring.jersey.type=filter
?我想保留 application.properties 仅用于真实环境基础配置。泽西岛过滤器模式永远不会改变。
spring.jersey.type=filter
是spring配置,不是球衣
我已经检查了这些扩展坞:https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-developing-web-applications.html 关于注册使用过滤器,但是失败了。
基本上 application.properties
你已经拥有的是去这里 IMO 的方式,这是一种 "straightforward" spring-引导配置方式。
您可以将此文件放入 src/main/resources
或 src/main/resources/config
(以防您在外部 运行 它会选取它。
现在如果你真的想在 java 中做到这一点,这里有几种方法:
选项 1:
@SpringBootApplication
public class MyApp{
public static void main(String[] args){
SpringApplication application = new SpringApplication(MyApp.class);
Properties properties = new Properties();
properties.put("spring.jersey.type", "filter");
application.setDefaultProperties(properties);
application.run(args);
}
}
选项 2
创建org.springframework.boot.env.EnvironmentPostProcessor
并在META-INF/spring.factories
中注册:
此环境的接口 post-processor 允许添加属性。
可以找到此类 post 处理器的示例 Here
选项 3
按照惯例,除了常规配置文件(开发、生产或您拥有的任何内容)之外,您的所有服务都将 运行 "jersey" 配置文件
然后您可以创建 application-jersey.properties
,只要您指定球衣配置文件 (--spring.profiles.active=dev,jersey
) 或以编程方式
,spring 启动应用程序就会自动选择它
@SpringBootApplication
public class MyApp
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("jersey");
app.run(args);
}
}
嗨,我在 application.properties 中有条目:
spring.jersey.type=filter
这允许我在过滤模式下 运行 带有 spring 的球衣,多亏了它,我可以共享 API 资源和静态内容,例如主页或 swagger 文档。
我知道,为了这项服务,我将始终 运行 这件球衣。所以它永远不会改变。我想使用 java 来配置它,比如 f.e。:这里:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
// scan the resources package for our resources
packages(getClass().getPackage().getName() + ".resources");
property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
}
是否可以使用 java 代码表示此 属性 条目:spring.jersey.type=filter
?我想保留 application.properties 仅用于真实环境基础配置。泽西岛过滤器模式永远不会改变。
spring.jersey.type=filter
是spring配置,不是球衣
我已经检查了这些扩展坞:https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-developing-web-applications.html 关于注册使用过滤器,但是失败了。
基本上 application.properties
你已经拥有的是去这里 IMO 的方式,这是一种 "straightforward" spring-引导配置方式。
您可以将此文件放入 src/main/resources
或 src/main/resources/config
(以防您在外部 运行 它会选取它。
现在如果你真的想在 java 中做到这一点,这里有几种方法:
选项 1:
@SpringBootApplication
public class MyApp{
public static void main(String[] args){
SpringApplication application = new SpringApplication(MyApp.class);
Properties properties = new Properties();
properties.put("spring.jersey.type", "filter");
application.setDefaultProperties(properties);
application.run(args);
}
}
选项 2
创建org.springframework.boot.env.EnvironmentPostProcessor
并在META-INF/spring.factories
中注册:
此环境的接口 post-processor 允许添加属性。
可以找到此类 post 处理器的示例 Here
选项 3
按照惯例,除了常规配置文件(开发、生产或您拥有的任何内容)之外,您的所有服务都将 运行 "jersey" 配置文件
然后您可以创建 application-jersey.properties
,只要您指定球衣配置文件 (--spring.profiles.active=dev,jersey
) 或以编程方式
@SpringBootApplication
public class MyApp
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("jersey");
app.run(args);
}
}