[Solved] Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

The front-end request enters the back-end controller and reports an error [cannot serialize instance of ` Java. Lang. string ` out of start_object token]. From the error information, it is a JSON parsing error. Compare the content sent by the front-end request and the parameters received by the back-end controller to find the problem.

Content sent by the front end:

api_save(JSON.stringify({
  po: this.form
})).then(response => {
  if (response.code === 200) {
    this.$message.success('success to save')
    this.$emit('saved')
  }
}).catch(() => {
  this.$message.error('failed to save')
})

Parameters received by the back-end controller:

@PostMapping("/save")
public Return<?> save(@RequestBody Map<String, String> paramMap) {
    try {
        service.save(JSON.parseObject(paramMap.get("po"), ProgEbs.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Return.success("save successfully");
}

After comparison, it is found that the front-end sending request is an object and the back-end controller receiving parameters are strings. Therefore, there is an error in JSON parsing at the back-end of the controller. At this time, just adjust the front-end sending request content to JSON strings or the back-end controller receiving parameters to object objects.

The former is used here:

api_save(JSON.stringify({
  po: JSON.stringify(this.form)
})).then(response => {
  if (response.code === 200) {
    this.$message.success('save successfully')
    this.$emit('saved')
  }
}).catch(() => {
  this.$message.error('failed to save')
})

This solves the problem of error reporting, and the back-end controller can receive parameters normally.

Similar Posts: