Home > Enterprise >  How to execute http requests in a Spring application without using WebFlux/RestTemplate (webmvc)?
How to execute http requests in a Spring application without using WebFlux/RestTemplate (webmvc)?

Time:09-29

In my Spring application, I need to perform GET and rarely POST requests. The application is standalone (not a WEB application). It will not process any incoming requests. It lacks controllers/routers, etc. I'm trying to find a "convenient" solution for making requests. I made a choice in the direction of WebFlux, but realized that in this case, this is the same thing that I connected to my webmvc application and it became a WEB application. I would like to avoid connecting so much of a large component, most of which will not be used. How would it be in this situation? Abandon WebFlux in the direction of java.net.http.HttpClient (Java 11) or another analog, or a bare HttpConnection?

Java 18, SpringBoot 2.7.5-SNAPSHOT

dependencies {
    // implementation 'org.springframework.boot:spring-boot-starter-webflux' ?
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    // testImplementation 'io.projectreactor:reactor-test' ?
}

CodePudding user response:

You can use java.net.http.HttpClient of Java 11 as you already mentioned. Using HttpConnection is an option but I wouldn't recommend it. In addition you can just use any 3d party Http client. Here are 2 most popular ones:

  1. Apache Http Client
  2. OK Http client

Besides those - and this is shameless self-promotion - I wrote my own Http client. I don't mean to compete with fullness of features with any of the above clients, but my client is very simple in use and does support basic functionality which in most cases is enough. It is part of MgntUtils Open source library written and maintained by me. Here is a Javadoc for HttpClient class. The library you can get as maven artifacts on Maven central here or on Github here with source code and Javadoc.

  • Related