HTTP POST Implementation for Sending Array Parameters with Java and Spring
When developing client applications that need to transmit collections of data to a server endpoint, utilizing an HTTP POST request is the standard approach. Specifically, scenarios involving batch operations often require sending an array of identifiers or objects. To handle this on the client side using Java's core libraries, one must configure the connection correctly and serialize the array into a format that the server can parse, typically JSON. On the server side, frameworks like Spring Boot offer seamless integration to map this incoming data directly into Java arrays or Collections via specific annotations.
Client-Side Request Configuration
The following implementation demonstrates how to establish a connection and transmit an array of Long values. The HttpURLConnection class is used to manage the connection settings. It is crucial to set the Content-Type header to application/ so the server knows how to interpret the request body. The data is serialized into a JSON array string format before being written to the output stream.
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class NetworkClient {
public static void transmitArrayData() throws IOException {
String targetUrl = "http://localhost:8080/api/v1/resources/process";
URL url = new URL(targetUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/");
connection.setDoOutput(true);
Long[] resourceIds = {101L, 102L, 103L};
String Payload = Arrays.toString(resourceIds);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = Payload.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
System.out.println("Server responded with status: " + responseCode);
} finally {
connection.disconnect();
}
}
}
Server-Side Data Ingestion
On the receiving end, a Spring MVC controller can be configured to accept the JSON payload. By using the @PostMapping annotation, the method listens for POST requests on the specified path. The @RequestBody annotation automatically binds the incoming JSON array to the method parameter. In this example, the controller accepts a String array, iterates over the elements, and processes them.
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/resources")
public class ResourceController {
@PostMapping("/process")
public ResponseEntity<String> handleBatchIds(@RequestBody String[] incomingIds) {
// Process the received array
Arrays.asList(incomingIds).forEach(id -> {
System.out.println("Processing ID: " + id);
// Business logic here
});
return ResponseEntity.ok("Batch processing completed successfully.");
}
}