1

I have a DTO with a couple fields, username, password and email All of them Strings.

I also have a List that I want as an optional, meaning it does not have to be passed in from the form and can be left blank.

This should be valid from the front-end:

{
"username":"user",
"email":"email@email.com",
"password": "123",
"hobbies":[ "Weightlifting", "Dancing"]
} 

So should this:

{
"username":"user",
"email":"email@email.com",
"password": "123"
} 

Is there any @OptionalField annotation or the likes within javax.validation.constraints?

Or is my only option two seperate DTOs?

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Lithicas
  • 3,793
  • 7
  • 23
  • 32
  • If you don't want hobbies list so add @transient annotation.if you use this annotation that particular field is not participate in serialization. – Umapathi Nov 27 '18 at 12:45
  • use `@JsonIgnoreProperties({"properties to ignore"})` on your DTO – The Coder Nov 27 '18 at 13:06
  • These comments don't appear to make the field optional, that appear to make it ignored. I think the question (certainly my question) is how to have a DTO with an _optional_ field. At the moment we are confronted with sending one of two different DTOs depending on whether we have the optional field data or not. – GreenAsJade Jan 30 '19 at 06:15
  • I think you are using same DTO for 2 purposes or APIs? In one the list is mandatory and in another is not? – ScanQR Jan 30 '19 at 06:19

1 Answers1

0

Solution in this case was to make the field an Optional. Example, Optional<Set<Hobby>> hobbies;

Lithicas
  • 3,793
  • 7
  • 23
  • 32