下面是我的 Spring Java 配置类。我想要它的 spring xml。我的困惑是,如何将@Bean HttpClient 转换为 xml(我需要使用工厂方法吗?)
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import com.google.common.collect.Lists;
@Configuration
public class RestClientConfig {
@Bean
public ClientHttpRequestFactory httpRequestFactory() throws GeneralSecurityException, IOException {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public HttpClient httpClient() throws GeneralSecurityException, IOException {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
TrustStrategy allTrust = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpClient;
}
@Bean
public RestTemplate restTemplate() throws GeneralSecurityException, IOException {
RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
List<ClientHttpRequestInterceptor> interceptors = Lists.newArrayList();
interceptors.add(new RestAuthInterceptor());
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}
::编辑:: 这是我在得到 Jose Luis Martin 和 MariuszS 的帮助后所做的。 XML 文件:
<bean id="httpClient" class="com.orbit.restclient.support.CustomHttpClientFactory" />
<bean class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
</bean>
</constructor-arg>
<property name="interceptors">
<list>
<bean class="com.orbit.restclient.support.RestAuthInterceptor" />
</list>
</property>
</bean>
自定义类:
public class CustomHttpClientFactory implements FactoryBean<HttpClient> {
@Override
public HttpClient getObject() throws Exception {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// TODO: update code here to validate certificate. This code allows all certificates
TrustStrategy allTrust = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpClient;
}
@Override
public Class<HttpClient> getObjectType() {
return HttpClient.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
最佳答案
尝试:
<bean id="httpClient" class="test.HttpClientFactoryBean" />
<bean id="httpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient"></constructor-arg>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="interceptors">
<list>
<bean class="RestAuthInterceptor" />
</list>
</property>
</bean>
和
public class HttpClientFactoryBean extends AbstractFactoryBean<HttpClient> {
@Override
public Class<?> getObjectType() {
return HttpClient.class;
}
@Override
protected HttpClient createInstance() throws Exception {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
TrustStrategy allTrust = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpClient;
}
}
关于java - 将 Spring java 配置转换为 xml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005762/