在启动类父文件夹中新建UserController.java
@RestController
public class UserController {
@GetMapping("/hello")
public void hello(Date birth){
System.out.println(birth);
}
}
浏览器输入
http://localhost:8080/hello?birth=1996-06-14
此时会出现参数类型错误
再新建MyDateConverter.java
@Component public class MyDateConverter implements Converter{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); @Override public Date convert(String source) { try { return sdf.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; } }
此时访问http://localhost:8080/hello?birth=2020-02-01
程序返回
POST 请求,参数可以是 key/value 形式,也可以是 JSON 形式。
自定义的类型转换器对 key/value 形式的参数有效。
JSON 形式的参数,不需要类型转换器。JSON 字符串是通过 MttpMessageConverten 转换为 User 对象的.



