gpt4 book ai didi

javascript - 通过 Axios 发送 post 请求会在 Spring-Boot 后端生成一个空的 RequestBody。在 Postman 中工作,但不能通过 Axios 发布请求

转载 作者:行者123 更新时间:2023-11-29 23:03:17 25 4
gpt4 key购买 nike

我有一个 React 前端和一个 Spring 后端。我为后端提供休息服务,它将 summarizerData 作为输入并返回与输出相同的内容。我有一个采用文本区域输入和提交按钮的表单。通过 axios 发送发布请求时,我得到一个空对象。我已经通过 postman 测试了 api,但是当通过 axios 提交它时,我收到了 500 内部错误。

我在 RestController 中启用了 CORS。

请告诉我问题是什么SummarizerData Pojo

@Entity(name = "user_text_data")
@Getter
@Setter
@ToString
public class SummarizerData {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column
private String paragraph;


@Column
private LocalDateTime creationDate;

@Transient
private List<Sentence> summarizedSentences;


public SummarizerData(){

}

public SummarizerData(String paragraph){
this.paragraph = paragraph;
this.creationDate = LocalDateTime.now();
}


}

TextSummarizerController

@RepositoryRestController
@RequestMapping("/api")
public class TextSummarizerController{

@Autowired
SummarizerDataRepository repository;

Logger logger = Logger.getLogger(TextSummarizerController.class.getName());


@CrossOrigin
@RequestMapping(method = RequestMethod.POST, value = "/summarize")
public @ResponseBody SummarizerData getSummarizerData(@RequestBody SummarizerData data ){
System.out.println("Returning Summarized Data");
SummaryTool summaryTool = new SummaryTool();

logger.info(data.toString());
repository.save(data);
data.setSummarizedSentences(summaryTool.startSummarization(data.getParagraph()));
return data;
}



}

React 前端

import React, { Component } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import Navbar from './components/Navbar';
import ParagraphEntry from './components/ParagraphEntry';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import axios from 'axios';
class App extends Component {



constructor(props){
super(props);

this.state = {
"summarizerData" : {},
"paragraph" : ""
} ;
}



onChange = (e) => {
this.setState({"paragraph" : e.target.value});
};

onSubmit= (e) => {
e.preventDefault();
var headers = {
'Content-Type': 'application/json'
}
const summarizerData = {
"paragraph" : this.state.paragraph,
"creationDate" : "2019-03-10T00:58:23",
"summarizedSentences" :null
};

axios.post('http://localhost:8080/api/summarize',{summarizerData}, {headers})
.then(res => console.log(res.data))
console.log(summarizerData);


}


handleClear = (e) => {
console.log(e);
e.target.value = "";
this.setState({"paragraph" : ""});

}
render() {
return (

<div className="App">

<Navbar />

<ParagraphEntry onChange = {this.onChange} onSubmit={this.onSubmit} handleClear = {this.handleClear} paragraph = {this.state.paragraph}/>


</div>

);
}
}

export default App;

错误日志

Returning Summarized Data
2019-03-18 00:10:19.487 INFO 8336 --- [nio-8080-exec-5] c.n.t.s.rest.TextSummarizerController : SummarizerData(id=null, paragraph=null, creationDate=null, summarizedSentences=null)
2019-03-18 00:10:19.491 WARN 8336 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1048, SQLState: 23000
2019-03-18 00:10:19.491 ERROR 8336 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'creation_date' cannot be null
2019-03-18 00:10:19.495 ERROR 8336 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

java.sql.SQLIntegrityConstraintViolationException: Column 'creation_date' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) ~[mysql-connector-java-8.0.15.jar:8.0.15]

上面的亮点是所有字段都为空的 SummarizerData。

我是一个 react 初学者,所以请让我知道任何错误。

谢谢

最佳答案

问题是您不必要地用对象构造函数运算符 ({}) 包装了 summarizerDataheaders。您之前已经创建了对象,这应该可以解决您的问题:

axios.post('http://localhost:8080/api/summarize', summarizerData, headers)

{summarizerData} 所做的是创建一个这样的对象:

{
"summarizerData": {
"paragraph": this.state.paragraph,
"creationDate": "2019-03-10T00:58:23",
"summarizedSentences": null
}
}

无法映射到后端的 SummarizerData。您可以使用 Devloper Tools(ChromeFirefox)来调查您的 HTTP 调用。它会让你例如要查看请求正文中实际发送的内容,请查看请求和响应 header 及其值等。

关于javascript - 通过 Axios 发送 post 请求会在 Spring-Boot 后端生成一个空的 RequestBody。在 Postman 中工作,但不能通过 Axios 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com