Hard java spring

Controller returns a JPA entity with a lazy OneToMany?

The 30 second answer

Jackson walks getters and touches the lazy collection. With open-in-view off you get LazyInitializationException wrapped as a message conversion error. With Spring Boot's default open-in-view true it loads during rendering, which causes extra queries, N-plus-one risk, possible infinite recursion on bidirectional mappings, and leaked internal fields. Return DTOs or records, fetch what you need up front, and prefer open-in-view false.

Jackson serializes getters → touches the lazy collection. With OSIV off: LazyInitializationException (wrapped as HttpMessageNotWritableException). With Spring Boot's default spring.jpa.open-in-view=true (it logs a warning): it loads silently → extra queries/N+1 during rendering, possible infinite recursion with bidirectional mappings (StackOverflow/JsonMappingException), and you leak internal fields. Fix: return DTOs/records, fetch what you need (JOIN FETCH, @EntityGraph), set open-in-view=false.

Sources:

Sources (0)
See inline citations in the answer above.