But how can I serialize lazy classes into JSON using the standard jackson that I added to my XML file.
First of all, I do not recommend using the DTO / Value Object just to solve this problem.
You can easily find this at the beginning, but with each new development / change, duplicate code means to make all the changes twice ... otherwise, errors.
I do not mean that VO or DTO are bad smells, but you should use them for the reasons they are designed (for example, providing a content / structure that differs according to logical levels or solving an unsolvable serialization problem).
If you have a clean and efficient way to solve the serialization problem without VO / DTO, and you don't need them, don't use them.
And about that, there are many ways to solve the lazy loading problem as you are using Jackson with Hibernate objects.
In fact, the easiest way is to use FasterXML / jackson-datatype-hibernate
Jackson (jar) module project to support JSON serialization and Hibernate deserialization ( http://hibernate.org ) data types and properties; especially lazy aspects.
It provides Hibernate3Module/Hibernate4Module/Hibernate5Module , extension modules that can be registered with ObjectMapper to provide a well-defined set of extensions related to the Hibernate specifications.
To do this, you just need to add the required dependency and add the Jackson module is available during processes where necessary.
If you are using Hibernate 3:
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate3</artifactId> <version>${jackson.version.datatype}</version> </dependency>
If you are using Hibernate 4:
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> <version>${jackson.version.datatype}</version> </dependency>
And so for ...
Where jackson.version.datatype should be the same for the version of Jackson used and the ackon-datatype extension.
If you use or can use Spring Boot, you just need to declare the module as a bean in a specific Configuration class or in a SpringBootApplication class, and it will be automatically registered for any Jackson ObjectMapper .
74.3 Customize the Jackson ObjectMapper Spring The download section states that:
Any beans of the type com.fasterxml.jackson.databind.Module will be automatically registered using the automatic Jackson2ObjectMapperBuilder configuration and applied to any ObjectMapper instances that it creates. This provides a global mechanism for facilitating custom modules when adding new features to your application.
For instance:
@Configuration public class MyJacksonConfig { @Bean public Module hibernate5Module() { return new Hibernate5Module(); } }
or:
@SpringBootApplication public class AppConfig { public static void main(String[] args) throws IOException { SpringApplication.run(AppConfig.class, args); } @Bean public Module hibernate5Module() { return new Hibernate5Module(); } }