Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

80
Visualizações
Spring Profile Based Jersey Rest Service publish

It is possible to publish jersey rest service based on spring profile? lets say as following example, how can I publish RegisterServices1 when using profile1?

public class ApiGWRestApplicationConfig extends ResourceConfig {

   public ApiGWRestApplicationConfig() {     
      register(RegisterServicesApiGWInterface.class);
    }
}

@Service
@Profile("profile1")
@Path(SystemConstants.REST_REGISTER)
public class RegisterServices1 implements RegisterServicesApiGWInterface {

}

@Service
@Profile("profile2")
@Path(SystemConstants.REST_REGISTER)
public class RegisterServices2 implements RegisterServicesApiGWInterface{}

web.xml

<servlet>
    <servlet-name>jersey-servlet-kagw</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.ttech.tims.imos.web.ApiGWRestApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

So what you can do is get a hold of the ApplicationContext and use getBeansWithAnnotation(Path.class). This will give you all the resource instances that are part of the profile. Then you can register the instances.

I though it would be possible to inject the ApplicationContext into the ResourceConfig, but as mentioned in the comment above, it seems the creation of the ResourceConfig doesn't have access to it yet.

What I was able to get to work, is to use a JAX-RS Feature which also has access to registration methods, just like you have in the ResourceConfig. Using the Feature will give you access to the ApplicationContext

public class SpringProfilesFeature implements Feature {

    @Inject
    private ApplicationContext context;

    @Override
    public boolean configure(FeatureContext featureContext) {
        Map<String, Object> resources = context.getBeansWithAnnotation(Path.class);

        resources.values().forEach(resource -> featureContext.register(resource));

        return true;
    }
}

Then just register the feature with the ResourceConfig

public AppConfig() {
    register(SpringProfilesFeature.class);
}

Remove any other registrations you have for all your resources. Just let the feature register them.

I've confirmed that this works. Not sure how you set your profile for the environment, but hopefully this is something you already know how to do.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda