• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Spring Boot Rest API JSON validation

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a rest API set up that accepts a JSON request and maps it to a POJO.  My question is, what is the best practice or how do I validate that the JSON that is being sent contains valid data before continuing on and processing further logic?  If it's invalid, I want to send a message back to the user to indicate that the request was bad.  Do I have to iterate through every possible field and check them?  For example, let's say I'm expecting a value for a field that will end up being a main data element for my java object, how can I verify it is there?  Thanks in advance for the help.
 
Ranch Hand
Posts: 82
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are describing is commonly known as (Java Bean Validation).
 
Ranch Hand
Posts: 1815
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my brainstorming suggestion.
Support you have a POJO called Student:


In your controller, you may have something like this:



You can create a Test package using JUnit 5:

 
Greg Abel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help
 
I once met a man from Nantucket. He had a tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!