简体   繁体   中英

Java spring boot, Lombok in POJO for response entity body

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 ?

@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;
}
  • Update/install Lombok plugin for Idea
  • Enable annotation process for Idea在此处输入图片说明
  • Enable annotation process for plugin在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM