springboot 中 HttpMessageNotWritableException 异常处理

前言:

先把报错日志贴出来:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.test.dto.ResultBean]

如果日志类似可以继续往下看。

1. 问题原因和处理

先还原事故现场(只展示核心问题代码)

接口层代码:

@RestController
@RequestMapping("/client")
public class ControllerTest {

    @GetMapping("/test")
    public ResultBean name() {
        return ResultBean.success();
    }
}

ResultBean 代码:

public class ResultBean {

    private String code;
    private String result;

    ResultBean(String code, String result) {
        this.code = code;
        this.result = result;
    }

    public static ResultBean success() {
        return new ResultBean("200", "success");
    }

}

出错日志指向了这个 ResultBean,大概率就是这个 bean 有问题,仔细分析那个日志,大概意思是没法获取值导致没法写,获取值?get方法?好像,ResultBean 确实少了get方法(lombok注解忘了),加上 get 方法试一试,问题果然解决了....

Tips:一定要加 get、set方法,尽量手写;lombok 注解少用,这里面有坑,偶尔特殊字段生成的默认方法没办法被其他组件识别。

你可能感兴趣的:(后端,spring,boot,HttpMessage,Not,Writable,Exception)