I am trying to migrate my REST client application to use WebClient instead of RestTemplate. But, when I run my client code, it is apparently trying to start a web server and connect to port 8080, which fails because Tomcat is already running on the port.
I don't want to start a web server. I just want to connect to an external web server and pull back a response.
Here is the error I get:
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
Here is my test code:
package test.rest.webClient;
import java.util.Map;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
import reactor.core.publisher.Mono;
import test.Path;
@SpringBootApplication
public class WebClientTest implements CommandLineRunner {
@Override
public void run(String... args)
throws Exception {
URIBuilder builder = new URIBuilder();
builder.setScheme("https");
builder.setHost("marketing.propfinancing.com");
builder.setPath("/caddata/TXCollin/getByIdAndYear");
builder.addParameter("id", "37");
builder.addParameter("year", "2022");
WebClient client = WebClient.create();
RequestHeadersUriSpec<?> uriSpec = client.get();
uriSpec.uri(builder.build());
uriSpec.header(Path.getApplicationProperties().getProperty("caddata.apiKey.header.name"),
Path.getApplicationProperties().getProperty("caddata.apiKey"));
uriSpec.accept(MediaType.APPLICATION_JSON);
ResponseSpec responseSpec = uriSpec.retrieve();
ParameterizedTypeReference<Map<String,Object>> typeReference = new ParameterizedTypeReference<Map<String,Object>>(){};
Mono<Map<String,Object>> mono = responseSpec.bodyToMono(typeReference);
Map<String,Object> response = mono.block();
for( Object key : response.keySet() ) {
Object value = response.get(key);
LoggerFactory.getLogger(getClass()).warn(key ":" value);
}
}
public static void main(String[] args)
throws Exception {
SpringApplication.run(WebClientTest.class, args);
}
}
Any ideas?
CodePudding user response:
Per default spring boot starts up either as a web application or a reactive application depending on what libraries you have on your classpath.
But you can also tell the framwork to not start up the webserver by explicitly setting the WebApplicationType
to None
here is an example:
new SpringApplicationBuilder(MainApplication.class)
.web(WebApplicationType.NONE)
.run(args);
or you can set it in the application properties:
spring.main.web-application-type=none
You can read more about it here: