Json com.fasterxml.jackson.databind.exc.MismatchedInputException:如何将JS中带有数组字段的对象映射到Java对象?
当我从客户端向服务器端发出请求时,我会传递带有数组字段的对象。在服务器端,我收到以下类型的错误消息:Json com.fasterxml.jackson.databind.exc.MismatchedInputException:如何将JS中带有数组字段的对象映射到Java对象?,json,reactjs,spring,rest,type-mismatch,Json,Reactjs,Spring,Rest,Type Mismatch,当我从客户端向服务器端发出请求时,我会传递带有数组字段的对象。在服务器端,我收到以下类型的错误消息: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.example.dto.TopicDto` out of START_ARRAY toke
Failed to read HTTP message:
org.springframework.http.converter.HttpMessageNotReadableException: JSON
parse error: Cannot deserialize instance of `com.example.dto.TopicDto`
out of START_ARRAY token; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
deserialize instance of `com.example.dto.TopicDto` out of START_ARRAY token
如何将此对象映射到Java POJO?我应该在客户端对这个对象使用JSON.stringify()或JSON.parse()方法吗
在出现错误之前:缺少必需的请求正文
我的代码如下:
@Controller
public class TopicController {
@PostMapping(name = "/topic/{forumType}/{jwtToken}")
public ResponseEntity<?> addTopicToForum(@RequestBody TopicDto topicDto, @PathVariable String forumType,
@PathVariable String jwtToken) {
}
}
客户端
class AddTopic extends React.Component {
constructor(props) {
super(props);
this.state = {
title: '',
message: '',
isInvalidTitle: false,
isInvalidMessage: false,
successFiles: [],
successNameFiles: [],
isSuccessUploaded: false,
isFailureUploaded: false,
failureNameFiles: []
}
this.onSubmitForm = this.onSubmitForm.bind(this);
this.responseFilestack = this.responseFilestack.bind(this);
}
onSubmitForm(e) {
e.preventDefault();
this.validateForm(e);
if(this.errors.length === 0) {
let jwtToken = localStorage.getItem("JwtToken");
let forumType = this.props.match.params.selectedForum;
const topicDto = {
title: this.state.title,
message: this.state.message,
likes: "0",
languageForum: forumType,
files: this.state.successFiles
}
fetch(`/topic/${forumType}/${jwtToken}`, {
method: 'POST',
body: topicDto,
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
}
}).then(response => {
console.log(response);
})
}
}
responseFilestack(response) {
response.filesUploaded.map((file, index) => {
const element = {
objectId: file.uploadId,
url: file.url,
size: file.size,
name: file.originalFile.name,
type: file.originalFile.type
};
this.setState({
successFiles: this.state.successFiles.concat(element),
successNameFiles: this.state.successNameFiles.concat(file.originalFile.name),
isSuccessUploaded: true
});
});
}
}
更新:
喜欢:“0”
像字符串一样返回值,但您希望使用INT。请附加您的请求正文而不是javascript代码。这个请求正文是您想要的吗?这是我附加的照片吗?我将likes:0改为likes:0,这与titleRequestBody中的错误相同。Body是随请求一起附加的内容。若你们点击preview标签,你们会在那个里看到JSON。这将有助于识别问题。预览选项卡为空
public class FileDto {
private String objectId;
private String url;
private Long size;
private String name;
private String type;
// getters and setters, no constructors
}
class AddTopic extends React.Component {
constructor(props) {
super(props);
this.state = {
title: '',
message: '',
isInvalidTitle: false,
isInvalidMessage: false,
successFiles: [],
successNameFiles: [],
isSuccessUploaded: false,
isFailureUploaded: false,
failureNameFiles: []
}
this.onSubmitForm = this.onSubmitForm.bind(this);
this.responseFilestack = this.responseFilestack.bind(this);
}
onSubmitForm(e) {
e.preventDefault();
this.validateForm(e);
if(this.errors.length === 0) {
let jwtToken = localStorage.getItem("JwtToken");
let forumType = this.props.match.params.selectedForum;
const topicDto = {
title: this.state.title,
message: this.state.message,
likes: "0",
languageForum: forumType,
files: this.state.successFiles
}
fetch(`/topic/${forumType}/${jwtToken}`, {
method: 'POST',
body: topicDto,
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
}
}).then(response => {
console.log(response);
})
}
}
responseFilestack(response) {
response.filesUploaded.map((file, index) => {
const element = {
objectId: file.uploadId,
url: file.url,
size: file.size,
name: file.originalFile.name,
type: file.originalFile.type
};
this.setState({
successFiles: this.state.successFiles.concat(element),
successNameFiles: this.state.successNameFiles.concat(file.originalFile.name),
isSuccessUploaded: true
});
});
}
}