I have such kind of @OneToOne Hibernate relationShip
public class Address implements Serializable {
private String id;
private String city;
priv
But how can I serialize to JSON lazy classes using standard jackson message converter which of cause I added to my XML file.
First of all, I don't advise to use DTO/Value Object only to solve this issue.
You may find it easy at the beginning but at each new development/change, the duplicate code means making twice modifications at each time... otherwise bugs.
I don't mean that VO or DTO are bad smells but you should use them for reasons they are designed (such as providing a content/structure that differs according to logical layers or solving an unsolvable serialization problem).
If you have a clean and efficient way to solve the serialization issue without VO/DTO and you don't need them, don't use them.
And about it, there is many ways to solve lazy loading issue as you use Jackson with Hibernate entities.
Actually, the simplest way is using FasterXML/jackson-datatype-hibernate
Project to build Jackson module (jar) to support JSON serialization and deserialization of Hibernate (http://hibernate.org) specific datatypes and properties; especially lazy-loading aspects.
It provides Hibernate3Module/Hibernate4Module/Hibernate5Module, extension modules that can be registered with ObjectMapper to provide a well-defined set of extensions related to Hibernate specificities.
To do it working, you just need to add the required dependency and to add the
Jackson Module available during processings where it is required.
If you use Hibernate 3 :
com.fasterxml.jackson.datatype
jackson-datatype-hibernate3
${jackson.version.datatype}
If you use Hibernate 4 :
com.fasterxml.jackson.datatype
jackson-datatype-hibernate4
${jackson.version.datatype}
And so for...
Where jackson.version.datatype should be the same for the used Jackson version and the ackson-datatype extension.
If you use or may use Spring Boot, you just need to declare the module as a bean in a specific Configuration class or in the SpringBootApplication class and it will be automatically registered for any Jackson ObjectMapper created.
The 74.3 Customize the Jackson ObjectMapper Spring Boot section states that :
Any beans of type
com.fasterxml.jackson.databind.Modulewill be automatically registered with the auto-configuredJackson2ObjectMapperBuilderand applied to anyObjectMapperinstances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.
For example :
@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();
}
}