json - 如何解决错误:无效的 JSON 输入:无法从 START_ARRAY 令牌中反序列化 Topic 实例
问题描述
我正在开发一个使用 Springboot Rest API 和 ReactJS 前端的调查应用程序,我面临一个看似无法解决的问题。在我的 Springboot 应用程序中,我有一个名为 Topic 的 bean,它的编写如下:
@Entity
public class Topic {
@Id
@GeneratedValue
private Long id;
private String question;
@NotNull
@Size(min=3, max=20)
private String topic;
@Column(precision=4, scale=1)
private double npm = 0;
@OneToMany(mappedBy = "topic")
private Set<Submission> submissions;
public Topic()
{
super();
}
public Topic(Long id, String topic, double npm)
{
super();
this.id = id;
this.setTopic(topic);
this.setNpm(npm);
}
public Long getID()
{
return this.id;
}
public void setId(Long id)
{
this.id = id;
}
/**
* @return the topic
*/
public String getTopic() {
return topic;
}
/**
* @param topic the topic to set
*/
public void setTopic(String topic) {
this.topic = topic;
}
/**
* @return the npm
*/
public double getNpm() {
return npm;
}
/**
* @param npm the npm to set
*/
public void setNpm(double npm) {
this.npm = npm;
}
/**
* @return the question
*/
public String getQuestion() {
return question;
}
/**
* @param question the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
}
和一个存储库类:
@Repository
public interface TopicRepository extends JpaRepository<Topic, Long>{
@Transactional
@Modifying
@Query("UPDATE Topic SET npm = ?1 WHERE id = ?2")
public void updateNpm(double newNpm, long id);
@Query("SELECT topic FROM Topic")
public List<String> getTopics();
}
最后,我的Controller类如下(我省略了不会导致问题的方法):
@RestController
public class TopicResource {
@Autowired
private TopicRepository topicRepository;
@CrossOrigin
@PostMapping("/topics")
public void createTopic(@RequestBody Topic topic)
{
Topic savedTopic = topicRepository.save(topic);
}
}
在我的 React 应用程序中,我从以下组件类发出 POST 请求:
import React, { Component } from 'react';
import './Styling/createsurvey.css'
class CreateSurvey extends Component {
constructor () {
super();
this.state = {
topic: '',
question: ''
};
this.handleTopicChange = this.handleTopicChange.bind(this);
this.handleQuestionChange = this.handleQuestionChange.bind(this);
//this.printState = this.printState.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleTopicChange(e) {
this.setState({
topic: e.target.value
});
}
handleQuestionChange(e) {
this.setState({
question: e.target.value
});
}
handleSubmit(event) {
event.preventDefault();
console.log(this.state.topic);
console.log(this.state.question);
fetch('http://localhost:8080/topics', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: {
"npm": "0",
"topic": "Turkey",
"question": "How is life here",
"submissions": {}
}
});
}
render() {
return(
<div>
<form onSubmit={this.handleSubmit}>
<h2>Create a new Survey</h2>
<br></br>
<div className="form-group">
<label><strong>Enter the survery topic: </strong></label>
<br></br>
<input
className="form-control"
placeholder="Enter a cool topic to ask a question about"
align="left"
onChange={this.handleTopicChange}>
</input>
</div>
<div className="form-group">
<label><strong>Enter a survey question: </strong></label>
<br></br>
<textarea
className="form-control"
placeholder="Ask your question here. The customer will give an answer on a scale from 1 to 10."
align="left"
rows="3"
onChange={this.handleQuestionChange}>
</textarea>
</div>
<div className="form-group">
<button type="submit" className="btn btn-primary">Submit Your Question</button>
</div>
</form>
</div>
);
}
}
export default CreateSurvey;
我面临以下错误:
2020-03-03 01:15:17.862 DEBUG 17816 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : POST "/topics", parameters={}
2020-03-03 01:15:17.862 DEBUG 17816 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.recommend.me.springboot.rest.recommendme.topic.TopicResource#createTopic(Topic)
2020-03-03 01:15:17.867 DEBUG 17816 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Could not resolve parameter [0] in public void com.recommend.me.springboot.rest.recommendme.topic.TopicResource.createTopic(com.recommend.me.springboot.rest.recommendme.topic.Topic): Invalid JSON input: Cannot deserialize instance of `com.recommend.me.springboot.rest.recommendme.topic.Topic` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.recommend.me.springboot.rest.recommendme.topic.Topic` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
有人可以指出我的请求做错了什么吗?我尝试了所有形式的 JSON 请求格式,但似乎没有任何效果。
解决方案
在将序列化的主题对象发送到控制器时,您需要对正文进行字符串化。JSON.stringify应该可以解决这个问题。
检查下面的代码并链接到jsfiddle。
fetch('http://localhost:8080/topics', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({
"npm": "0",
"topic": "Turkey",
"question": "How is life here",
"submissions": {}
})
})
.then(function (response) {
return response.json();
})
.then(function (result) {
alert(result)
})
.
catch (function (error) {
console.log('Request failed', error);
});
推荐阅读
- android - Flutter:想要在按下返回按钮或按下按钮时清理数据
- javascript - 使用javascript修剪并返回特定字符集删除其余部分
- javascript - 复选框未在 If/Else 函数中激活
- docker - 您是否需要从“/usr/src/app/”更新 dockerfile 中的路径?
- vba - VBA 检测用户何时执行撤消或重做
- flutter - Flutter BLoC 监听其他区块
- c++ - 在 Visual Studio C++ 中,如何一次解码一帧视频,并一次创建另一个一帧?
- r - 等效于 data.table 中的 dplyr rowwsie combn
- powershell - 如何在powershell中清除多个局部变量
- rust - 使用 activitystreams crate 获取 Mastodon 配置文件的发件箱