Article directory
1. Reproduce the error
I finished writing the interface today 页面按钮排序
, as shown in the following code:
@ApiOperationSupport(author = "super先生", order = 8)
@ApiOperation(value = "页面按钮排序")
@PostMapping("/sort/pageButton")
public ReturnResult sortPageButton(
@Validated @RequestBody SortPageButtonDto sortPageButtonDto,
BindingResult bindingResult) {
BindingParamUtil.checkParam(bindingResult);
return appPageButtonService.sortPageButton(sortPageButtonDto);
}
When using postman
the test, the following error is reported:
At this point, immediately check the error message reported by the console, as shown in the following code:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.xxx.SortPageButtonDto` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.xxx.SortPageButtonDto` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:389)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:185)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:170)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at com.github.xiaoymin.knife4j.spring.filter.SecurityBasicAuthFilter.doFilter(SecurityBasicAuthFilter.java:87)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
......
Since there are many error messages, we extract the main error messages, namelyCannot deserialize instance of 'com.xxx.SortPageButtonDto' out of START_ARRAY token at [Source: (PushbackInputStream); line: 1, column: 1]
2. Analysis errors
It is catching up with the recent ChatGPT
fire, and I use it to help me analyze errors, as shown in the following figure:
ChatGPT
Nothing is returned, that is, I can't understand my problem. It seems that I can only troubleshoot errors by myself.
Look at the error message again Cannot deserialize instance of 'com.xxx.SortPageButtonDto' out of START_ARRAY token
and simply translate it into Chinese无法反序列化SortPageButtonDto类的实例。
Why can't an instance of a class be deserialized SortPageButtonDto
?
First, go to the troubleshooting SortPageButtonDto
class, as shown in the following code:
@AllArgsConstructor
@NoArgsConstructor
@Data
@ApiModel(description = "页面按钮排序请求类")
public class SortPageButtonDto {
@Size(min = 1)
@ApiModelProperty(name = "sortPageButtons", value = "页面按钮请求参数", required = true)
private List<SortPageButton> sortPageButtons;
@ApiModel(description = "查询条件请求类")
@Data
public class SortPageButton {
@NotNull(message = "id不能为空")
@ApiModelProperty(name = "id", value = "页面按钮id", required = true, example = "1")
private Long id;
@NotNull(message = "排序值不能为空")
@ApiModelProperty(name = "seq", value = "排序值", required = true, example = "1")
private Long seq;
}
}
This classSortPageButtonDto
is used to receive parameters in the array format passed by the front end, as shown in the following code:
[
{
"id": 1,
"seq": 20
},
{
"id": 2,
"seq": 21
}
]
Therefore, I SortPageButtonDto
define an SortPageButton
inner class in the class. SortPageButton
The class is used as an SortPageButtonDto
attribute of the class to receive the array passed by the front end.
There is no problem in writing this way, but the above error is reported, so I searched for information everywhere.
According to some information, json
there is a problem with the above format and needs to be modified to the following format:
{
"sortPageButtonDto": [
{
"id": 1,
"seq": 20
},
{
"id": 2,
"seq": 21
}
]
}
After modifying to the above format, use the postman
calling 页面按钮排序
interface again. Although no error is reported, the backend cannot receive the parameter, that is, null
the value, as shown in the following figure:
Since the above method does not work, use the following method to solve it.
3. Solve the problem
Try to modify SortPageButtonDto
the class, delete SortPageButton
the inner class, as shown in the following code:
@AllArgsConstructor
@NoArgsConstructor
@Data
@ApiModel(description = "页面按钮排序请求类")
public class SortPageButtonDto {
@NotNull(message = "id不能为空")
@ApiModelProperty(name = "id", value = "页面按钮id", required = true, example = "1")
private Long id;
@NotNull(message = "排序值不能为空")
@ApiModelProperty(name = "seq", value = "排序值", required = true, example = "1")
private Long seq;
}
At the same time, modify the interface code started above /sort/pageButton
as follows:
@ApiOperationSupport(author = "super先生", order = 8)
@ApiOperation(value = "页面按钮排序")
@PostMapping("/sort/pageButton")
public ReturnResult sortPageButton(
@Validated @RequestBody List<SortPageButtonDto> sortPageButtonDto,
BindingResult bindingResult) {
BindingParamUtil.checkParam(bindingResult);
return appPageButtonService.sortPageButton(sortPageButtonDto);
}
At this point, restart the project and use postman
the test again, but still report the same error, as shown in the following figure:
Restore the request parameters in the format in the above figure json
to the format of the initial request json
, and send the request to the backend again, as shown in the figure below:
It can be clearly seen from the above figure that after the above modification, the backend can successfully receive the array parameters passed by the frontend.
After repeated attempts, I finally solved the error, turned off the computer with a sigh of relief, and went home from get off work.
4. Summary at the end of the paper
When this kind of error occurs, it is generally used by the backend 集合或数组
to receive parameters from the frontend, as shown in the following code:
//集合接参
@Size(min = 1)
@ApiModelProperty(name = "sortPageButtons", value = "页面按钮请求参数", required = true)
private List<SortPageButton> sortPageButtons;
//数组接参
@Size(min = 1)
@ApiModelProperty(name = "sortPageButtons", value = "页面按钮请求参数", required = true)
private SortPageButton[] sortPageButtons;
However, due to irregular parameter passing, Jackson
the actual parameter value cannot be obtained when parsing the data, thus reporting the above error.
Therefore, when solving this error, the front and back ends need to be modified together. Otherwise, if the backend is modified but the frontend is not modified, the above error will still be reported.