I am trying to use Ajax posts to a Spring @RestController
. Jackson has issues with @Json...Reference
annnotations inside the @Entity
the posted one inherits from (@Inheritance(strategy = InheritanceType.JOINED)
).
I've seen plenty of questions and answers regarding that. As of now, permanently changing the parent entity is not allowed. I do have access to, and can temporarily change, my local copy so I've been able to confirm removing the annotations in the parent could potentially fix it, presumably, at the cost of breaking something else. So, my solution is to implement a custom JsonSerializer
.
The problem with that is it doesn't get called; I think I've done it correctly from what I've seen. Here's the relative code:
I register the @Bean
in my AppConfig
file. I've tried variations for the method body; this is just the most recent one that doesn't work...
@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
SimpleModule m = new SimpleModule();
m.addSerializer(Orchard.class, new OrchardSerializer());
return new Jackson2ObjectMapperBuilder().modules(m);
}
I provide the serializer; the break point below is never triggered...
public class OrchardSerializer extends JsonSerializer<Orchard> {
@Override
public Class<Orchard> handledType() {
return Orchard.class;
}
@Override
public void serialize(Orchard orchard, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws JsonProcessingException {
System.out.println("Break on me...");
}
}
The entity I'm trying to post...
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@JsonSerialize(using = OrchardSerializer.class)
public class Orchard extends Yard {
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Person> contacts;
@Column(name = "COUNT")
private Integer count;
...getters/setters
}
How do I register a custom JsonSerializer
so it will be called on an Ajax post?
The JsonSerializer handles the serialization and deserialization of raw json data into java objects.
@JsonSerialize is used to specify custom serializer to marshall the json object.
The Jackson Object mapper can parse JSON into objects of classes developed by you, or into objects of the built-in JSON tree model explained later in this tutorial. By the way, the reason it is called ObjectMapper is because it maps JSON into Java Objects (deserialization), or Java Objects into JSON (serialization).
It seems like you're not using Spring Boot. AFAIK Without Spring Boot Jackson2ObjectMapperBuilder
are not automatically detected. If you want to add a custom serializer you should do it in WebMvcConfigurerAdapter
.
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
SimpleModule m = new SimpleModule();
m.addSerializer(Orchard.class, new OrchardSerializer());
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().modules(m);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With