이 어노테이션을 dto 로 만든 클래스 위에 붙여주지 않으면, response 로 보내줄 때
MessageConversionException: Could not write JSON: No serializer found for class ... and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
...to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS
https://steady-hello.tistory.com/90
- (1) serializer 하는 과정에서 기본으로 접근 제한자가 public 이거나
- (2) getter/setter를 이용하기 때문에 인스턴스 필드를 private 등으로 선언하면 json으로 변환 과정에서 에러가 발생
위와 같이 response로 줄 데이터를 직렬화하지 못한다고 한다.
이는 필드가 존재하지 않는 것을 직렬화할 때 나는 에러라고 한다.
그렇다면 @Data를 빼면 필드가 존재하지 않는다는 것인데, @Data
의 역할은 과연 무엇일까?
/**
* Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
* <p>
* Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for @Data</a>.
*
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
/**
* If you specify a static constructor name, then the generated constructor will be private, and
* instead a static factory method is created that other classes can use to create instances.
* We suggest the name: "of", like so:
*
* <pre>
* public @Data(staticConstructor = "of") class Point { final int x, y; }
* </pre>
*
* Default: No static constructor, instead the normal constructor is public.
*
* @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
*/
String staticConstructor() default "";
}
Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.