[英]Spring Profile Based Jersey Rest Service publish
可以根據春季個人資料發布運動衫休息服務嗎? 讓我們說以下示例,使用profile1
時如何發布RegisterServices1
?
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
並使用getBeansWithAnnotation(Path.class)
。 這將為您提供配置文件中的所有資源實例。 然后,您可以注冊實例。
我雖然可以將ApplicationContext
注入到ResourceConfig
,但是如上面的注釋中所述,似乎ResourceConfig
的創建尚無法訪問它。
我能夠使用的工作是使用JAX-RS Feature
,該Feature
也可以訪問注冊方法,就像ResourceConfig
。 使用Feature
將使您可以訪問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);
}
刪除所有資源的所有其他注冊。 只需讓功能注冊它們即可。
我已經確認這可行。 不確定如何設置環境配置文件,但希望這是您已經知道的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.