-2

To address the same issue as here RestTemplate PATCH request in my spring-boot 3 application

org.springframework.web.client.ResourceAccessException: I/O error on PATCH request for "http://localhost:8080/people/1":Invalid HTTP method: PATCH; nested exception is java.net.ProtocolException: Invalid HTTP method: PATCH

I do things similar as in accepted response:

@Configuration
class TestRestTemplateConfig {

    @Bean
    fun testRestTemplate(restTemplateBuilder: RestTemplateBuilder): TestRestTemplate {
        val testRestTemplate = TestRestTemplate(restTemplateBuilder, null, null, null)
        testRestTemplate.restTemplate.requestFactory = HttpComponentsClientHttpRequestFactory()
        return testRestTemplate
    }
}

Also I have dependency:

implementation("org.apache.httpcomponents:httpclient:4.5.14")

But when I start test I see an error:

Caused by: java.lang.ClassNotFoundException: org.apache.hc.client5.http.classic.HttpClient

But If I try to find this class in my IDE I am able to do it:

enter image description here

I tried to execute command gradle clean but no luck. I am confused.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

1

The solution is replace

implementation("org.apache.httpcomponents:httpclient:4.5.14")

with

Implementation("org.apache.httpcomponents.client5:httpclient5")
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • The real solution is to let Spring Boot manage your dependencies instead of doing it manually. – Mark Rotteveel Jul 26 '23 at 16:27
  • @Mark Rotteveel could you be a bit more specific please ? – gstackoverflow Jul 26 '23 at 16:27
  • You're not doing things according to how Spring Boot should be used, if you'd let Spring Boot manage the dependencies, it would either have imported the correct dependencies automatically or you wouldn't have needed to specify the version of dependencies which Spring Boot manages but does not depend on by default. – Mark Rotteveel Jul 26 '23 at 16:30
  • What should I do to let Spring Boot manage my dependencies ? specifically this one – gstackoverflow Jul 26 '23 at 16:32
  • https://docs.spring.io/spring-boot/docs/3.1.2/gradle-plugin/reference/htmlsingle/ – Mark Rotteveel Jul 26 '23 at 16:40
  • Actually I have `plugins { java kotlin("jvm") kotlin("plugin.spring") version "1.8.21" id("io.spring.dependency-management") version "1.1.0" ` – gstackoverflow Jul 26 '23 at 16:45
  • 1. Tried to remove `testImplementation("org.apache.httpcomponents.client5:httpclient5:5.2.1")` But no luck - ClassNotFoundException – gstackoverflow Jul 26 '23 at 16:49
  • 2. Without version also works ` testImplementation("org.apache.httpcomponents.client5:httpclient5")` – gstackoverflow Jul 26 '23 at 16:52
  • The linked document says you should use `id 'org.springframework.boot' version '3.1.2'`... – Mark Rotteveel Jul 27 '23 at 07:16
  • @Mark Rotteveel oh, it is so fresh. let me try – gstackoverflow Jul 27 '23 at 07:53
  • @Mark Rotteveel for my 3.1.0 - result is the same – gstackoverflow Jul 27 '23 at 08:02
  • Which starters do you have specified? – Mark Rotteveel Jul 27 '23 at 08:12
  • @MarkRotteveel ` implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") implementation("org.springframework.boot:spring-boot-starter-security") ` – gstackoverflow Jul 27 '23 at 08:19
  • Turns out I was wrong specifically about Apache HTTP client being pulled in automatically... my apologies. Spring will use it if it is on the classpath, but otherwise fallback to HttpUrlConnection. But in any case, specifying versionless should always be preferred for dependencies managed by Spring Boot (and Apache HTTP client is) – Mark Rotteveel Jul 27 '23 at 08:51
  • Thank you for your report. I've removed version. Looks like it improved the answer. – gstackoverflow Jul 27 '23 at 08:58