0

i'm using spring boot, jackson and hibernate for a basic backend with an rest (more or less rest..) api.

Now the issue is, that i'd like to use the entity and not wanna use a dto to keep complexity low.

The issue is, that when i retrieve an entity with a collection, jackson will fetch stuff and yes there is a circle in it.

What i would prefer is to be able to use spring data, jackson and the entity class but controlling the associations manually.

I don't want to configure it with JsonIgnore or other annotations.

Tx!

3
  • Can you please share some code? Commented Aug 7, 2018 at 20:40
  • "and not wanna use dto" - How are you gong to handle Serialization? Don't you want to hide your representational data? DTO's were not made to make life harder, but to encompass the underpinnings of your entities. Commented Aug 7, 2018 at 20:45
  • @Dan it is a small project with a clear scope. There is no need for it. DTOs just add another code bloat in this case. Commented Aug 8, 2018 at 11:09

2 Answers 2

1

Couple of ways to do this

  1. Use @JsonIgnore
  2. Use @JsonManagedReference (something like this) and @JsonBackReference
  3. My favorite - use @JsonView

What for?

  1. Will exclude unwanted properties from serialization/deserialization
  2. Is made explicitly to solve circular dependencies serialization/deserialization
  3. Keeps your payload as small as possible - this allows you to pick which properties should be serialized for given endpoint.

Which one is best suited for you?

Well its up to you, but from the description, looks like 2 is the way to go (maybe combined with 3) Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

DTOs are the way to go in more complex scenarios, especially on the inbound side. For dynamic filtering of simpler use cases i wrote an addon for jackson to use antpath style filtering. Probably it helps you:

https://github.com/Antibrumm/jackson-antpathfilter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.