Search code examples
javaspringweb-servicesspring-ws

Conditionally expose Spring web service operation


We're using Spring web services in our application and have a requirement to make a new operation, intended for internal testing, available only in our dev and test environments. I know how to handle such a requirement in Axis (we have one such module, where we simply add or remove the operation from the "allowedMethods" parameter in our wsdd) but I have no idea how to accomplish this in Spring, and haven't had much luck searching online. What are our options?


Solution

  • Spring profiles will help you:

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

    @Configuration
    @Profile("dev")
    public class ProductionConfiguration {
    
        // ...
    
    }
    

    You can use profiles on a Bean:

    @Component
    @Profile("dev")
    public class DevDatasourceConfig
    

    Or in the XML:

    <beans profile="dev">
        <bean id="devDatasourceConfig"
          class="org.baeldung.profiles.DevDatasourceConfig" />
    </beans>
    

    You could also specify what profiles are in use by WebApplicationInitializer:

    @Configuration
    public class MyWebApplicationInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.setInitParameter("spring.profiles.active", "dev");
        }
    } 
    

    or

    @Autowired
    private ConfigurableEnvironment env;
    ...
    env.setActiveProfiles("dev");
    

    or web.xml:

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>
    

    or JVM parameter:

    -Dspring.profiles.active=dev
    

    or environment variable:

    export spring_profiles_active=dev