Java Spring Boot,使用Lombok在响应实体主体中的POJO。

huangapple go评论75阅读模式
英文:

Java spring boot, Lombok in POJO for response entity body

问题

我为API创建了一个默认错误响应体的对象。
因此,我创建了一个类,声明了构造函数和参数,并且我使用lombok添加了@Getter@Setter

@Getter
@Setter
public class ResponseError {
    private Date timestamp;
    private int status;
    private int code;
    private String message;
    
    public ResponseError(Date timestamp, int status, int code, String message) {
        this.timestamp = timestamp;
        this.status = status;
        this.code = code;
        this.message = message;
    }   
}

当我在ResponseEntity的响应体中初始化对象时,构建时出现了以下错误:

No converter found for return value of type: class com.example.api.controller.response.ResponseError

如果我在我的类中手动创建getter和setter,就可以工作。
我以为lombok会为我做这个,难道不是吗?

英文:

I create an object for default error response body for API.
So I create my class, i declare constructor and params and I add @Getter and @Setter from lombok.

@Getter
@Setter
public class ResponseError {
	private Date timestamp;
    private int status;
    private int code;
    private String message;
    
    public ResponseError(Date timestamp, int status, int code, String message) {
		this.timestamp = timestamp;
		this.status = status;
		this.code = code;
		this.message = message;
	}   
}

When i initiate the object in body response of ResponseEntity, i got this error on building :

No converter found for return value of type: class com.example.api.controller.response.ResponseError

And if i create manually getter and setter in my class, it is working.
I thought that Lombok do this for me, don't it ?

答案1

得分: 0

@Getter/@Setter 应该应用于一个字段。\

这应该是你的代码

public class ResponseError {

    @Getter
    @Setter
    private Date timestamp;

    @Getter
    @Setter
    private int status;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String message;
}

对于类级别,甚至更好的解决方案是使用 @Data 注解。

它为所有字段生成 getter 方法,一个有用的 .toString 方法,以及 .hashCode.equals 实现,用于检查所有非瞬态字段。
它还将为所有非 final 字段生成 setter 方法,以及一个构造函数。
它相当于拥有所有这些注解:@Getter@Setter@RequiredArgsConstructor@ToString@EqualsAndHashCode

如果你想使用 @Data,你的代码将会是这样的。

@Data
public class ResponseError {
    private Date timestamp;
    private int status;
    private int code;
    private String message;
}
英文:

@Getter/@Setter should be applied to a field.
This should be your code

public class ResponseError {

    @Getter
    @Setter
    private Date timestamp;

    @Getter
    @Setter
    private int status;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String message;
}

Event better solution for class level, would be to use @Data annotation.

It generates getters for all fields, a useful .toString method, and .hashCode and .equals implementations that check all non-transient fields.
It will also generate setters for all non-final fields, as well as a constructor.
It is equivalent to having all annotations: @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode

If you wish to use @Data, your code would be.

@Data
public class ResponseError {
    private Date timestamp;
    private int status;
    private int code;
    private String message;
}

答案2

得分: 0

  • 为Idea更新/安装Lombok插件

  • 启用Idea的注解处理
    Java Spring Boot,使用Lombok在响应实体主体中的POJO。

  • 启用插件的注解处理
    Java Spring Boot,使用Lombok在响应实体主体中的POJO。

英文:
  • Update/install Lombok plugin for Idea

  • Enable annotation process for Idea
    Java Spring Boot,使用Lombok在响应实体主体中的POJO。

  • Enable annotation process for plugin
    Java Spring Boot,使用Lombok在响应实体主体中的POJO。

huangapple
  • 本文由 发表于 2020年9月6日 18:50:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63763278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定