FetchType=LAZY 지연로딩을 사용하면 프록시 객체만 가져오기 때문에 객체를 serializer 할 때 오류가 발생한다.
1번 방법은 오류를 발생시키지 않게 할 뿐, 오류가 발생하는 근본적인 원인은 해결하지 못한다.
As it is correctly suggested in previous answers, lazy loading means that when you fetch your object from the database, the nested objects are not fetched (and may be fetched later when required).
Now Jackson tries to serialize the nested object (== make JSON out of it), but fails as it finds JavassistLazyInitializer instead of normal object. This is the error you see. Now, how to solve it?
As suggested by CP510 previously, one option is to suppress the error by this line of configuration:
spring.jackson.serialization.fail-on-empty-beans=false
But this is dealing with the symptoms, not the cause. To solve it elegantly, you need to decide whether you need this object in JSON or not?
Should you need the object in JSON, remove the FetchType.LAZYoption from the field that causes it (it might also be a field in some nested object, not only in the root entity you are fetching).
If do not need the object in JSON, annotate the getter of this field (or the field itself, if you do not need to accept incoming values either) with @JsonIgnore, for example:
// this field will not be serialized to/from JSON @JsonIgnore private NestedType secret;
Should you have more complex needs (e.g. different rules for different REST controllers using the same entity), you can use jackson views or filtering or for very simple use case, fetch nested objects separately.