Ignoring Hibernate Garbage via @JsonIgnoreProperties

As I was building out some model entities in a Spring application I'm working on, I began to build out the front-end piece (in Angular 2) to view some of the data and when I would query this specific entity over its RESTful endpoint I kept getting a CORS error. The reason this was odd to me was because all my other endpoints were working and they all flow through the same filter that provides the appropriate CORS headers.

I decided to switch over to Postman and see if it would give different results since it was just hitting the URL rather than going through JavaScript. When I did this, my application threw up all over my screen and I was left with the following clue:

HTTP Status 500 - Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap["departments"]->java.util.UnmodifiableRandomAccessList[0]  

What this told me was that there was something in my models that couldn't be serialized by Jackson. And clearly these "somethings" were Hibernate related. After doing some googling I found this Q&A on Stackoverflow so I applied the following to my app's configuration:

public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {  
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter(mapper);
    return converter;
} 

Hitting the endpoint via Postman again I got the following:

{
    "departments": [{
        "id": 8,
        "name": "Alias in i",
        "departmentGroup": {
            "id": 4,
            "name": "Earum dese",
            "departmentType": {
                "id": 3,
                "name": "Cumque cul",
                "handler": {},
                "hibernateLazyInitializer": {}
            },
            "handler": {},
            "hibernateLazyInitializer": {}
        }
    }]
}

Progress! But wait. My models don't have handler and hibernateLazyInitializer properties. Back to google I found this issue reported which wasn't a bug or anything at all. You just need to know how to tell your serializer to ignore the helpful garbage that Hibernate adds to your classes so it can manage lazy loading of data.

In my case, I just had to annotate my models with Jackson's @JsonIgnoreProperties like so:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Department {  
...
}

And since I'm ignoring these properties I can remove the mappingJackson2HttpMessageConverter() method I added to the app config.

{
    "departments": [{
        "id": 8,
        "name": "Alias in i",
        "departmentGroup": {
            "id": 4,
            "name": "Earum dese",
            "departmentType": {
                "id": 3,
                "name": "Cumque cul",
             }
         }
    }]
}

Perfection! Interestingly this only cropped up because of a @OneToOne relationship. My other relationships are mostly @ManyToOne and @OneToMany which don't seem to need the Hibernate garbage for whatever reason.