参考地址:https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc
源码地址(持续更新中):https://gitee.com/qinshizhang/spring-mvc-learn
入参的POJO对象如下:(注:以下内容都是Spring MVC基于注解的Java配置,而非基于xml配置文件开发)
public class RequestInfo implements Serializable {
private String username;
private Long userId;
private int userAge;
private double userHeight; // 身高
private Date birthday;
private BigDecimal balance; // 余额
// 为了不占篇幅,省略了get和set方法
}
Controller层代码如下:
@RestController
@RequestMapping(path = "v1/hello/")
public class HelloController {
@PostMapping(path = "showRequestInfo")
public RequestInfo showRequestInfo(@RequestBody RequestInfo requestInfo) {
return requestInfo;
}
}
请求参数如下:
### Send POST request with json body
POST http://localhost:9090/v1/hello/showRequestInfo
Content-Type: application/json
{
"username" : "java",
"userId" : "123455",
"userAge" : "23",
"userHeight" : "170.5",
"birthday" : "1990-12-31 23:59:59",
"balance" : "20.55"
}
请求响应结果如下:(部分)
<body><h1>HTTP状态 415 - 不支持的媒体类型</h1>
<hr class="line"/>
<p><b>类型</b> 状态报告</p>
<p><b>描述</b> 源服务器拒绝服务请求,因为有效负载的格式在目标资源上此方法不支持。</p>
<hr class="line"/>
<h3>Apache Tomcat/9.0.41</h3></body>
跟踪代码调试,发现抛出异常的地方在如下方法中:
org.springframework.web.servlet.mvc.metho