gpt4 book ai didi

java - Jersey web.xml 到基于注释的配置以及 HK2 绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 09:59:04 25 4
gpt4 key购买 nike

如何使用 Jersey 和 HK2 绑定(bind)将基于 web.xml 的配置迁移到基于注释的配置。

最佳答案

注意:请确保您在 pom 文件中使用相同的版本

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.25.1</version>
</dependency>

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 Binding分享这个类只是为了展示HK2绑定(bind)是如何完成的以及register(new 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);
}

}

关于java - Jersey web.xml 到基于注释的配置以及 HK2 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55767921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com