基于Spring Profile的Jersey Rest Service发布 [英] Spring Profile Based Jersey Rest Service publish

查看:86
本文介绍了基于Spring Profile的Jersey Rest Service发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据春季个人资料发布运动衫休息服务? 可以说,例如,使用profile1时如何发布RegisterServices1?

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>

推荐答案

因此,您所要做的就是掌握ApplicationContext并使用

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.

我可以将ApplicationContext注入到ResourceConfig中,但是正如

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.

我能够上班的是使用JAX-RS Feature,它也可以访问注册方法,就像您在ResourceConfig中一样.使用Feature将使您可以访问ApplicationContext

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

然后只需在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.

这篇关于基于Spring Profile的Jersey Rest Service发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆