@Configuration
public class MyWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
@Override
public FormattingConversionService mvcConversionService() {
FormattingConversionService f = super.mvcConversionService();
f.addFormatter(new DateFormatter("yyyy-MM-dd"));
return f;
}
}
@RestController
public class TestController {
@GetMapping
public Date test(Date date) {
return date;
}
}
When we access http://localhost:8080?date=2021-09-04, the argument type is converted through the DateFormatter's parse method, which relies on the SpringMVC framework to do the conversion. I wonder if the print method can also be invoked through the framework to return a string.
Do we need to manually invoke the print method, for example
@RestController
public class TestController {
@Resource
private FormattingConversionService conversionService;
@GetMapping
public String test(Date date) {
return conversionService.convert(date, String.class);
}
}