• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring Boot Rest API JSON validation

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 100
4
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are describing is commonly known as (Java Bean Validation).
 
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help
 
reply
    Bookmark Topic Watch Topic
  • New Topic