当前位置:   article > 正文

from Array value (token `JsonToken.START_ARRAY`); nested exception is com.fasterxml.jackson.databind

from array value (token `jsontoken.start_array`)

项目场景:

Axios发送数组请求后端接口报错400
{
"message": "Request failed with status code 400",
"name": "AxiosError",
"stack": "AxiosError: Request failed with status code 400\n    at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:21:12)\n    at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:120:79)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"adapter": [
"xhr",
"http"
],
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 10000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"token": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhMTliY2IyNWQ3MzU0YjgyODk3NzM3MDZiMDJlMDVhYiIsInN1YiI6IjMwIiwiaXNzIjoic2ciLCJpYXQiOjE3MTAxMjU3NDgsImV4cCI6MTcxMDEyOTM0OH0.G-ExDCVqjHgWFMlbuMTRV7HgDKnRGy5zwfXMvnP7vms"
},
"baseURL": "http://localhost:9090",
"url": "/user",
"method": "post",
"data": "[24]"
},
"code": "ERR_BAD_REQUEST",
"status": 400
}


问题描述

SpringBoot警告内容:Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type com.xx.xx.xx.User from Array value (token JsonToken.START_ARRAY); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type com.xx.xx.xx.User from Array value (token JsonToken.START_ARRAY) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]]

后端代码:

    @DeleteMapping
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public ResponseEntity<Object> removeUser(@RequestBody List<Integer> ids) {
        return new ResponseEntity<>(userService.removeUser(ids), HttpStatus.OK);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

原因分析:

使用postman请求后端没有警告,使用axios请求报错。
在这里插入图片描述
在这里插入图片描述

这是一个Jackson库的异常,表示在反序列化JSON时发生了输入不匹配的错误。可能是JSON格式不正确或与Java对象不匹配。需要检査JSON格式和Java对象的匹配性。


解决方案:

前端:axios请求方法更换为method: ‘delete’

export function XXXXX(url, data) {
  return request({
    url: url,
    method: 'delete',
    data: data
  })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

后端代码:

    @DeleteMapping
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public ResponseEntity<Object> removeUser(@RequestBody List<Integer> ids) {
        return new ResponseEntity<>(userService.removeUser(ids), HttpStatus.OK);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/530631
推荐阅读
相关标签
  

闽ICP备14008679号