异常1
2021-06-24 12:32:20.137 WARN 20024 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

错误代码1
login.html,采用了Thymeleaf
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/login" th:action="@{/login}" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
UserController.java

本文主要介绍了在SpringBoot应用中遇到的两个表单提交错误。第一个错误是由于前端使用表单提交,后端使用@RequestBody导致的。解决方案包括修改后端代码或前端发送JSON对象的字符串。第二个错误是因为Ajax请求contentType未设置为'application/json',导致'Unsupported Media Type'异常,解决办法是在Ajax设置contentType为'application/json'。

8633

被折叠的 条评论
为什么被折叠?



