简体   繁体   中英

How to configure with java @Configuration Spring to run jersey in filter mode

Hi I have entry in application.properties:

spring.jersey.type=filter

that allows me to run jersey with spring in filter mode, and thanks to that I can share API Resources and static content, like home page or swagger docs.

I know that for this service I will always run this jersey that way. So it will never change. I would like to configure it using java like fe: here:

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);
    }
}

Is it possible to express using java code this property entry: spring.jersey.type=filter ? I would like to keep application.properties only for real environment base configuration. Jersey filter mode will never be changed.

spring.jersey.type=filter is spring configuration, not jersey

I've checked those docks: https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-developing-web-applications.html about registering Using filter, but fail on this.

Basically application.properties that you already have is the way to go here IMO, this is a "straightforward" spring-boot way of doing configuration.

You can place this file into src/main/resources or src/main/resources/config (in case you run it externally and it will pick it.

Now if you really want to do it in java, here are a couple of ways:

Option 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);
  }
}

Option 2

Create org.springframework.boot.env.EnvironmentPostProcessor and register it in META-INF/spring.factories : The interface of this environment post-processor allows adding properties.

An example of such a post-processor can be found Here

Option 3

By convention decide that all your services will run "jersey" profile in addition to the regular profile (dev, prod, or whatever you have there)

Then you can create application-jersey.properties and it will be picked by spring boot application automatically as long as you specify jersey profile ( --spring.profiles.active=dev,jersey ) or programmatically

@SpringBootApplication
public class MyApp
 public static void main(String[] args) {
   SpringApplication app = new SpringApplication(MyApp.class);
   app.setAdditionalProfiles("jersey"); 
   app.run(args);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM