问题描述
通过PostMan
访问接口写入数据成功,但是返回结果却是406。
控制台返回:
2023-11-19 09:35:26.303 WARN 13172 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
PostMan
返回:
{
"timestamp": "2023-11-19T01:35:26.304+00:00",
"status": 406,
"error": "Not Acceptable",
"path": "/order/save"
}
问题分析
经排查发现接口返回类R中没有实现getter
和setter
。
解决办法
方法一
- 添加
lombok
依赖。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- 添加注解
@Data
。
@Data
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
private boolean success;
private String msg;
private int code;
private T data;
}
方法二
在R实体类中添加getter
和setter
方法。
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
private boolean success;
private String msg;
private int code;
private T data;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}