admin管理员组文章数量:1391818
I have a react frontend and a spring backend. I have rest service for the backend that takes a summarizerData as an input and returns the same as output. I have a form which takes a textarea input and submit button. When sending a post request through axios i am getting an empty object. I have tested the api through postman but when submitting it through axios, i am getting a 500 internal error.
I have enabled CORS in the RestController.
Please let me know what is the issue 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 FrontEnd
import React, { Component } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import Navbar from './ponents/Navbar';
import ParagraphEntry from './ponents/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;
Error logs
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 .mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) ~[mysql-connector-java-8.0.15.jar:8.0.15]
The highlight in the above is SummarizerData with all fields as null.
I am a react beginner so pls let me know of any mistakes.
Thanks
I have a react frontend and a spring backend. I have rest service for the backend that takes a summarizerData as an input and returns the same as output. I have a form which takes a textarea input and submit button. When sending a post request through axios i am getting an empty object. I have tested the api through postman but when submitting it through axios, i am getting a 500 internal error.
I have enabled CORS in the RestController.
Please let me know what is the issue 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 FrontEnd
import React, { Component } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import Navbar from './ponents/Navbar';
import ParagraphEntry from './ponents/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;
Error logs
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 .mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at .mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) ~[mysql-connector-java-8.0.15.jar:8.0.15]
The highlight in the above is SummarizerData with all fields as null.
I am a react beginner so pls let me know of any mistakes.
Thanks
Share Improve this question edited Mar 17, 2019 at 19:02 Vineet B Nath asked Mar 17, 2019 at 18:57 Vineet B NathVineet B Nath 531 silver badge4 bronze badges1 Answer
Reset to default 4The problem is that you unnecessarily wrap summarizerData
and headers
with object constructor operator ({}
). You have already created the objects before and this should resolve your issue:
axios.post('http://localhost:8080/api/summarize', summarizerData, headers)
What {summarizerData}
does is create an object like this:
{
"summarizerData": {
"paragraph": this.state.paragraph,
"creationDate": "2019-03-10T00:58:23",
"summarizedSentences": null
}
}
Which cannot be mapped to SummarizerData
in your backend. You can use Devloper Tools (Chrome, Firefox) to investigate your HTTP calls. It will allow you e.g. to see what is actually sent in request body, see request and response headers and it's values etc.
本文标签:
版权声明:本文标题:javascript - Sending a post request through Axios is generating an empty RequestBody in Spring-Boot backend. Works in Postman bu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744695645a2620260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论