The following example demonstrates handling an HTTP POST request submitted from a static HTML page.
Example
Java Config
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
public class MyWebConfig implements WebMvcConfigurer {
@Bean
public ExampleController exampleController() {
return new ExampleController();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {MyWebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
Static HTML Page with a POST Form
src/main/webapp/static/simple-form.html<html>
<body>
<h2>Person Details</h2>
<form action="/example-handler" method="post">
<label for="personName">Name:</label><br>
<input type="text" id="personName" name="personName"><br>
<label for="personAddress">Address:</label><br>
<input type="text" id="personAddress" name="personAddress"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Spring MVC controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/example-handler")
public class ExampleController {
@PostMapping
@ResponseBody
public String handlePostRequest(
@RequestParam("personName") String personName,
@RequestParam("personAddress") String personAddress) {
return String.format("simple response. name: %s, address: %s", personName, personAddress);
}
}
Running the Example
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
Access static page at http://localhost:8080/static/simple-form.html
Fill form:
On submitting form
To submit form parameters via POST using curl, you can use the -d (for standard form data):
$ curl -s http://localhost:8080/example-handler -d "personName=Joe&personAddress=Texas" simple response. name: Joe, address: Texas
Integration Test
package com.logicbig.example;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.web.context.WebApplicationContext;
@SpringJUnitWebConfig(MyWebConfig.class)
class ExampleControllerTest {
@Autowired private WebApplicationContext context;
private MockMvcTester mockMvcTester;
@BeforeEach
void setup() {
mockMvcTester = MockMvcTester.from(context);
}
@Test
void testHandlePostRequest() {
assertThat(
mockMvcTester
.post()
.uri("/example-handler")
.param("personName", "John Doe")
.param("personAddress", "123 Main St")
.exchange())
.hasStatusOk()
.bodyText()
.isEqualTo("simple response. name: John Doe, " + "address: 123 Main St");
}
}
mvn clean test -Dtest="ExampleControllerTest" Output$ mvn clean test -Dtest="ExampleControllerTest" [INFO] Scanning for projects... [INFO] [INFO] --< com.logicbig.example:spring-mvc-simple-form-submission-post-example >-- [INFO] Building spring-mvc-simple-form-submission-post-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ spring-mvc-simple-form-submission-post-example --- [INFO] Deleting D:\example-projects\spring-mvc\form-submission\spring-mvc-simple-form-submission-post-example\target [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ spring-mvc-simple-form-submission-post-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\form-submission\spring-mvc-simple-form-submission-post-example\src\main\resources [INFO] [INFO] --- compiler:3.15.0:compile (default-compile) @ spring-mvc-simple-form-submission-post-example --- [INFO] Recompiling the module because of changed source code. [INFO] Compiling 3 source files with javac [debug target 25] to target\classes [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-mvc-simple-form-submission-post-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\form-submission\spring-mvc-simple-form-submission-post-example\src\test\resources [INFO] [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ spring-mvc-simple-form-submission-post-example --- [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 2 source files with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ spring-mvc-simple-form-submission-post-example --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ExampleControllerTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.738 s -- in com.logicbig.example.ExampleControllerTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.197 s [INFO] Finished at: 2026-04-04T22:00:44+08:00 [INFO] ------------------------------------------------------------------------ INFO: Completed initialization in 3 ms
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.3.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- spring-test 7.0.6 (Spring TestContext Framework)
- hamcrest 3.0 (Core API and libraries of hamcrest matcher framework)
- assertj-core 3.26.3 (Rich and fluent assertions for testing in Java)
- junit-jupiter-engine 6.0.3 (Module "junit-jupiter-engine" of JUnit)
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- JDK 25
- Maven 3.9.11
|