简体   繁体   English

Spring 框架 HttpMessageNotReadableException:缺少所需的请求正文

[英]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.

相关问题 org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文 Spring 启动项目 - org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing Spring boot project 尝试发出发布请求时“缺少 HttpMessageNotReadableException 必需的请求正文” - “HttpMessageNotReadableException Required request body is missing” when trying to make a post request org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文 - org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing ExceptionHandlerExceptionResolver :已解决 [org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文: - ExceptionHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: 缺少所需的请求正文:在 spring 启动和 angular 9 - Required request body is missing:in spring boot and angular 9 无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:提取调用中缺少必需的请求正文 - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing From a Fetch Call Spring Boot执行器在跟踪中添加请求主体会抛出HttpMessageNotReadableException - Spring boot actuator adding request body in trace throws HttpMessageNotReadableException 缺少必需的请求正文 - Required request body is missing Spring boot post rest调用“缺少所需的请求正文” - Spring boot post rest call "Required request body is missing" 春季-无法读取HTTP消息。 缺少所需的请求正文 - Spring - Failed to read HTTP message. Required request body is missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM