Skip to content
Advertisement

No serializer found when serializing one Object

I’m trying to return an Object as JSON. Using the /user/id endpoint, I want to display a User based on his Id. When calling this controllerMethod I get the following Exception:

JavaScript

My contollerClass looks like this:

JavaScript

I checked if al fields have a public getter and tried various options with @JSONIgnoreProperties, but I can’t find it. Displaying all users as a JSONlist does work JSONlist with /user/list. So the problem is only there when trying to display one Object, not a list of Objects. From the repository it does find the User, but it’s unable to serialize that Object and put in on the screen.

The User class itself looks like this:

JavaScript

How can I display my User returned from /user/id?

A Solution?

As suggested below, I made it work using a Dto and ModelMapper.

I added

JavaScript

ControllerMethod

JavaScript

And UserDto

JavaScript

Now I’m able to show a User on the screen. Still I’m wondering if there is no solution using Jackson and without modelmapper and dto?

Advertisement

Answer

Maybe it’s not a good idea to use your entity (User) to expose the data about user via REST? Can you create UserDTO for your user that will implement Serializable and send this DTO via REST? In this case it should be necessary to convert User object that you’ve retrieved from the db to UserDTO.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement