Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly register a custom JsonSerializer?

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?

like image 911
ChiefTwoPencils Avatar asked Mar 11 '16 19:03

ChiefTwoPencils


People also ask

What is JsonSerializer in Java?

The JsonSerializer handles the serialization and deserialization of raw json data into java objects.

What is the use of @JsonSerialize?

@JsonSerialize is used to specify custom serializer to marshall the json object.

What is Jackson serializer?

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).


1 Answers

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()));
    }
}
like image 170
Tuan Pham Avatar answered Oct 06 '22 09:10

Tuan Pham