I try to configure restTemplate to call https url in spring boot 3 but that does not compile. For this i use import org.apache.http.impl.client.HttpClients import org.apache.http.ssl.SSLContexts and the code below :
fun restTemplate(): RestTemplate {
val restTemplate = RestTemplate()
// Load the .p12 file from the test resources directory
val p12File = ClassPathResource("certs/clientErfin2.p12").file
val p12Stream = FileInputStream(p12File)
try {
// Load the .p12 file into a KeyStore
val keyStore = KeyStore.getInstance("PKCS12")
keyStore.load(p12Stream, "password".toCharArray())
// Create an SSL context with the KeyStore
val sslContext = SSLContexts.custom()
.loadKeyMaterial(keyStore, "password".toCharArray())
.build()
// Create an HttpClient with the SSL context
val httpClient = HttpClients.custom()
.setSSLContext(sslContext)
.build()
// Create a ClientHttpRequestFactory with the HttpClient
val requestFactory: ClientHttpRequestFactory = HttpComponentsClientHttpRequestFactory(httpClient)
// Set the ClientHttpRequestFactory on the RestTemplate
restTemplate.requestFactory = requestFactory
} finally {
p12Stream.close()
}
return restTemplate
}
the line val requestFactory: ClientHttpRequestFactory = HttpComponentsClientHttpRequestFactory(httpClient) does not compile because of Cannot access class 'org.apache.hc.client5.http.classic.HttpClient'. Check your module classpath for missing or conflicting dependencie. How can i fix this?
Thank's!
i tried to add the dependency hc.client5 and that does not resolve the problem