0

I need to communicate between a Spring Boot application and a Java ServeSocket. I have I created the Java ServerSocket and the Spring Boot.

Java ServerSocket has a BufferedReader as an input and OutputStream as a response.

I have just 2 questions:

  1. How to send a Get Request from Spring Boot to a server?
  2. How to send a Get Request from Spring Boot to Java ServerSocket, if the first method can be applied in this case?
Puce
  • 37,247
  • 13
  • 80
  • 152

1 Answers1

0

The best way to send a GET request is to use a high-level API. With Spring Boot this is currently WebClient, which replaced the older RestTemplate (see also tutorials such as: https://www.baeldung.com/spring-5-webclient or https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/io.html#io.rest-client.webclient).

To call a server you need a URL such as <scheme>:<scheme-specific-part>. As you say you want a GET call I assume you mean a HTTP GET call, which makes the either <scheme> the protocol http or https and the <scheme-specific-part> takes the form //<server-address>:<port>. So in a simple scenario where you run the server socket on your localhost you could use something like http://localhost:<port>/<path>, where <port> is the port the ServerSocket listens to and <path> is the resource you want to GET.

Of course, a ServerSocket is very low-level. It's much more convenient to use either a higher-level API such as a HttpServlet or a high-level API such as RestController, which means to just provide another Spring Boot application for the server part.

Puce
  • 37,247
  • 13
  • 80
  • 152