minte9
LearnRemember



App

Create a web Spring application.
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

POST

Send a post request to /post_page API entry point.
 
/**
 * Spring Boot is the most popular way to start Spring projects.
 * Spring Boot is built on top of the spring framework.
 *
 * Start by creating a new Spring Web project ... 
 * by opening intializer on start.spring.io
 */

package com.minte9.post_request;

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @GetMapping("/")
    public String makeRequest() throws Exception {

        String url = "http://localhost:8080/post_page";
        String a   = "Hello";
        String b   = "World";

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .POST(HttpRequest.BodyPublishers.ofString(
                    "a=" + URLEncoder.encode(a, "UTF-8") + "&" + 
                    "b=" + URLEncoder.encode(b, "UTF-8")))
                .build();

        HttpResponse<String> response = client.send(
            request, HttpResponse.BodyHandlers.ofString()
        );

        String out = ""
            + "Response code: " + response.statusCode() + " / "
            + "POST vars: " + response.body();

        return out; 
            // Response code: 200 / POST vars: a = Hello, b = World
    }

    @PostMapping("/post_page")
    public String handleDemoRequest(
                    @RequestParam("a") String a, 
                    @RequestParam("b") String b) {
        
        String out = "a = " + a + ", b = " + b;
        return out;
    }
}



  Last update: 335 days ago