Following example shows how to customize data binding to map query parameters by using @InitBinder and a custom PropertyEditor.
Example
Spring Controller
@Controller
@RequestMapping("/")
public class TradeController {
@InitBinder("tradeDate")
public void customizeBinding(WebDataBinder binder) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
}
@GetMapping("/trade")
@ResponseBody
public String handleRequest(@RequestParam("tradeDate") Date tradeDate) {
return "request received for " + tradeDate;
}
}
Config class
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
}
Running Example
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
$ curl -s http://localhost:8080/trade?tradeDate=2017-01-10 request received for Tue Jan 10 00:00:00 CST 2017
Integration Tests
package com.logicbig.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.web.context.WebApplicationContext;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitWebConfig(MyWebConfig.class)
public class TradeControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvcTester mockMvc;
@BeforeEach
public void setUp() {
mockMvc = MockMvcTester.from(webApplicationContext);
}
@Test
public void testValidDateRequest() throws Exception {
String tradeDate = "2020-02-29";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date expectedDate = formatter.parse(tradeDate);
assertThat(mockMvc.get().uri("/trade")
.param("tradeDate", tradeDate))
.hasStatusOk()
.hasBodyTextEqualTo("request received for " + expectedDate);
}
@Test
public void testInvalidDateRequest() {
assertThat(mockMvc.get().uri("/trade")
.param("tradeDate", "12/25/2024"))
.hasStatus(HttpStatus.BAD_REQUEST);
}
}
mvn clean test -Dtest="TradeControllerTest" Output$ mvn clean test -Dtest="TradeControllerTest" [INFO] Scanning for projects... [INFO] [INFO] -------< com.logicbig.example:request-param-web-binding-example >------- [INFO] Building request-param-web-binding-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ request-param-web-binding-example --- [INFO] Deleting D:\example-projects\spring-mvc\request-param-web-binding-example\target [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ request-param-web-binding-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\request-param-web-binding-example\src\main\resources [INFO] [INFO] --- compiler:3.7.0:compile (default-compile) @ request-param-web-binding-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 3 source files to D:\example-projects\spring-mvc\request-param-web-binding-example\target\classes [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ request-param-web-binding-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\request-param-web-binding-example\src\test\resources [INFO] [INFO] --- compiler:3.7.0:testCompile (default-testCompile) @ request-param-web-binding-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\example-projects\spring-mvc\request-param-web-binding-example\target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ request-param-web-binding-example --- [INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider [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.TradeControllerTest [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.474 s -- in com.logicbig.example.TradeControllerTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.646 s [INFO] Finished at: 2026-05-08T22:30:46+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.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)
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- junit-jupiter-engine 6.0.3 (Module "junit-jupiter-engine" of JUnit)
- hamcrest 3.0 (Core API and libraries of hamcrest matcher framework)
- assertj-core 3.26.3 (Rich and fluent assertions for testing in Java)
- JDK 25
- Maven 3.9.11
|