Программисты JAVA общаются здесь
Anonymous
Переход с httpclient на httpclient5, ошибки в файле Java [закрыто]
Сообщение
Anonymous » 27 ноя 2025, 10:01
При переходе от зависимости httpclient к httpclient5 (я вставлю зависимость ниже) мой первоначальный код Java-файла (который я прикрепил) работал следующим образом: Позже я заметил, что моя сборка не удалась, потому что SSLConnectionSocketFactory устарел. И альтернативная функция, которую они предложили, тоже не работает. Так что, если кто-нибудь поможет мне обновить этот Java-код для работы с httpclient5, это будет очень полезно.
Код: Выделить всё
package com.ofs.fsgbu.analytics.runcmd;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import com.ofs.fsgbu.analytics.runcmd.utility.AAIRestUtil;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.ofs.fsgbu.analytics.runcmd.interceptor.UserPreferencesInterceptor;
@Configuration
@EnableAsync
@ComponentScan(basePackages = {"com.ofs.aai","com.ofs.fsgbu.analytics.commonlib"})
public class AppConfig implements WebMvcConfigurer{
@Autowired
UserPreferencesInterceptor userPreferencesInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
List\ patterns = new ArrayList\();
patterns.add("/rest-api/v1/run-cmd/oracle/sqlplus/");
registry.addInterceptor(userPreferencesInterceptor);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
SSLConnectionSocketFactory socketFactory = null;
try {
socketFactory = new SSLConnectionSocketFactory(AAIRestUtil.getSSLContext());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
return builder
.requestFactory(() -\> new HttpComponentsClientHttpRequestFactory(httpClient))
.setConnectTimeout(Duration.ofMillis(3000))
.setReadTimeout(Duration.ofMillis(3000))
.build();
}
}введите здесь описание изображения
Подробнее здесь:
https://stackoverflow.com/questions/798 ... -java-file
1764226904
Anonymous
При переходе от зависимости httpclient к httpclient5 (я вставлю зависимость ниже) мой первоначальный код Java-файла (который я прикрепил) работал следующим образом: Позже я заметил, что моя сборка не удалась, потому что SSLConnectionSocketFactory устарел. И альтернативная функция, которую они предложили, тоже не работает. Так что, если кто-нибудь поможет мне обновить этот Java-код для работы с httpclient5, это будет очень полезно. [code]package com.ofs.fsgbu.analytics.runcmd; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.util.ArrayList; import java.util.List; import com.ofs.fsgbu.analytics.runcmd.utility.AAIRestUtil; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.ofs.fsgbu.analytics.runcmd.interceptor.UserPreferencesInterceptor; @Configuration @EnableAsync @ComponentScan(basePackages = {"com.ofs.aai","com.ofs.fsgbu.analytics.commonlib"}) public class AppConfig implements WebMvcConfigurer{ @Autowired UserPreferencesInterceptor userPreferencesInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { List\ patterns = new ArrayList\(); patterns.add("/rest-api/v1/run-cmd/oracle/sqlplus/"); registry.addInterceptor(userPreferencesInterceptor); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { SSLConnectionSocketFactory socketFactory = null; try { socketFactory = new SSLConnectionSocketFactory(AAIRestUtil.getSSLContext()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (KeyManagementException e) { throw new RuntimeException(e); } CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(socketFactory) .build(); return builder .requestFactory(() -\> new HttpComponentsClientHttpRequestFactory(httpClient)) .setConnectTimeout(Duration.ofMillis(3000)) .setReadTimeout(Duration.ofMillis(3000)) .build(); } [/code] }введите здесь описание изображения Подробнее здесь: [url]https://stackoverflow.com/questions/79830986/changing-from-httpclient-to-httpclient5-errors-in-java-file[/url]