it seems this question on stackoverflow. https://stackoverflow.com/questions/50086003/unable-to-test-a-literal-in-request-parameter-with-testresttemplate
The problem is that when I send a request with a time formatted query parameter, Spring returns a 400 status code. Here's the code block and error information.
UT code block:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class ExampleControllerTest {
@Autowired private TestRestTemplate restTemplate;
@LocalServerPort private int port;
@Test
public void testPost(){
String url = "http://127.0.0.1:"
+ port
+ baseUrl
+ "/test-time?day=" + URLEncoder.encode("2023-12-08T11:44:22+08:00");
ResponseEntity<SensorsdataCommonHttpApiResult> responseEntity =
restTemplate.postForEntity(url,
buildBody(),
MyResp.class);
}
}
Controller code block:
@RequestMapping(value = "/test-time", method = RequestMethod.POST)
public ResponseEntity<MyResp> testTime(
@Valid
@RequestParam(value = "enum_info", required = false)
String enumInfo,
@Valid
@RequestParam(value = "day", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
OffsetDateTime day,
@Valid @RequestBody(required = false)
MyBody myBody) {
...
}
error info:
2024-01-04T16:13:27.281+0800 WARN 31836 http-nio-auto-1-exec-1 [XX-123] o.s.w.s.m.support.DefaultHandlerExceptionResolver []: Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.OffsetDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @javax.validation.Valid @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime] for value '2024-01-04T15:41:59.544862 08:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2024-01-04T15:41:59.544862 08:00]]`
Comment From: bclozel
Closing this issue in favor of the StackOverflow question. I'll have a look but I don't think this is a Spring Boot bug.