如何使用Jersey和HK2绑定将基于web.xml的配置迁移到基于注释的配置。
答案 0 :(得分:1)
web.xml (需要迁移)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="false" version="3.1"> <servlet> <servlet-name>accRestService</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>org.test.ciam.acctmgt</param-value> </init-param> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.bitguiders.ciam.acct.MyAppConfig</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>accountManagementRestService</servlet-name> <url-pattern>/acc/api/*</url-pattern> </servlet-mapping> </web-app>
MyAppConfig (需要迁移/更新)
package org.bitguiders.acc.cfg; import org.bitguiders.api.exceptions.GenericExceptionMapper; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; public class MyAppConfig extends ResourceConfig { public ApplicationConfig() { register(JacksonFeature.class); register(GenericExceptionMapper.class); register(new DependencyBinder()); } }
ApplicationConfig.java正在用新的基于Java / Annotation的配置类替换web.xml和MyAppConfig.java。
package org.bitguiders.acc.cfg import javax.ws.rs.ApplicationPath; import org.bitguiders.acc.api.exceptions.GenericExceptionMapper; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; @ApplicationPath("acc/api/*") public class ApplicationConfig extends ResourceConfig { public ApplicationConfig() { packages("org.bitguiders.acc"); register(JacksonFeature.class); register(GenericExceptionMapper.class); register(new DependencyBinder()); } }
HK2绑定共享此类,以演示如何进行HK2绑定以及注册(新DependencyBinder())的目的是什么
package org.bitguiders.acc.cfg;
import javax.inject.Singleton;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
public class DependencyBinder extends AbstractBinder{
private static final String UNSECURED = "unsecured";
private static final String SECURED = "secured";
@Override
protected void configure() {
bind(AccountManagementServiceImpl.class).to(AccountManagementService.class).in(Singleton.class);
bindFactory(AccessTokenStoreFactory.class).proxy(true).proxyForSameScope(false).to(AccessTokenStore.class).in(RequestScoped.class);
bind(CASLocationServiceImpl.class).to(LocationService.class).named(CAS_LOCATION_SERVICE).in(Singleton.class);
bindAsContract(LocationServiceFactory.class).in(Singleton.class);
bindFactory(AccessTokenProviderFactory.class).to(AccessTokenProvider.class).to(new TypeLiteral<Provider<String>>(){}).in(Singleton.class);
bind(CiamScim2ClientRequireTokenImpl.class).to(CiamScim2Client.class).named(SECURED).in(Singleton.class);
bind(CiamScim2ClientImpl.class).to(CiamScim2Client.class).named(UNSECURED).in(Singleton.class);
bind(AccountManagementOptionsServiceImpl.class).to(AccountManagementOptionsService.class).named(SECURED).in(Singleton.class);
bindAsContract(OptionsApiServiceFactory.class).in(Singleton.class);
}
}