当前位置:   article > 正文

org.springframework.http.converter.HttpMessageNotReadableException异常解决

org.springframework.http.converter.httpmessagenotreadableexception

问题现场还原

最近研发过程中,后台代码是这么写的:

    @ApiImplicitParam(name = "X-Token", value = "登陆用户Token",required = true, dataType = "String",paramType="header")
    @PostMapping(value = "/update-advert-status.do")
    public VueElementAdminResponse updateAdvertStatus(@RequestBody AdvertUpdateParam advertUpdateParam){
          ...
  }
  • 1
  • 2
  • 3
  • 4
  • 5

实体类是这么写的:

import lombok.Data;
/**
 * @author qing-feng.zhao
 */
@Data
public class AdvertUpdateParam{
    private String id;
    private Integer status;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

后台出现了这个错误:

2020-08-14 06:55:10.141 WARN  --- o.s.w.s.m.s.DefaultHandlerExceptionResolver --- org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:199:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (PushbackInputStream); line: 1, column: 1]); nested exception is com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (PushbackInputStream); line: 1, column: 1])
 at [Source: (PushbackInputStream); line: 3, column: 14]]
  • 1
  • 2
  • 3

解决方案

一种原因是页面传参和后台不符异常org.springframework.http.converter.HttpMessageNotReadableException

另外一种是被@RequestBody 修饰的请求参数实体类没有实现序列化接口,如果不序列化就会出错。

修复代码如下所示:

import lombok.Data;

import java.io.Serializable;

/**
 * @author qing-feng.zhao
 */
@Data
public class AdvertUpdateParam implements Serializable {
    private static final long serialVersionUID = -1242493306307174690L;
    private String id;
    private Integer status;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

本篇完~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/516258
推荐阅读
相关标签
  

闽ICP备14008679号