[英]Spring framework HttpMessageNotReadableException: Required request body is missing
I'm trying to make a post request in React to a backend server in Spring
, but keep getting the error bellow:我正在尝试在 React 中向
Spring
的后端服务器发出 post 请求,但不断收到以下错误:
[ERROR] - org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing.
[错误] - org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文。
It works fine on postman.它在邮递员上工作正常。
Code from the react part:反应部分的代码:
let testeDTO = JSON.stringify({
email: "teste@@teste.com",
senha: "1234",
grupo: 1
});
axios
.post("http://192.168.0.49:8080/site/login/", testeDTO, {
headers: {
"Content-Type": "application/json"
}
})
.then(response => {
console.log(response);
})
.catch(erro => {
console.log(erro.response);
});
Annotations on the rest controller:其余控制器上的注释:
@RestController
@RequestMapping("/site/login")
Code from Spring:春天的代码:
@PostMapping
public testeDTO login(@RequestBody TesteDTO testeDTO) {
}
CORS class: CORS 类:
@WebFilter(urlPatterns = {"/*"})
public class CORSFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "*, Content-Type, authorization, grupo, usuario");
filterChain.doFilter(request, response);
}
}
DTO code: DTO代码:
public class TesteDTO {
private String email;
private String senha;
private Long grupo;
//getters and setters
}
Apparently the header on the axios post needed to be wrapped in {}.显然 axios 帖子上的标题需要包含在 {} 中。 So, after way too long this solved for me, leaving it here for those who stumble in this question with a similar problem.
所以,经过很长时间这对我来说解决了,把它留在这里给那些在这个问题上遇到类似问题的人。
var headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST',
};
axios
.post("http://192.168.0.49:8080/site/login/", testeDTO, {headers})
.then(response => {
console.log(response);
})
.catch(erro => {
console.log(erro.response);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.