DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring RESTful Web Services Validation: A Complete Blueprint

Spring RESTful Web Services Validation: A Complete Blueprint

In this post, let's discuss how to develop a REST API and validate request data.

Sanjay Patel user avatar by
Sanjay Patel
CORE ·
Jul. 30, 18 · Tutorial
Like (9)
Save
Tweet
Share
23.93K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous post, we discussed how to handle exceptions in a Spring Boot Web MVC application. Building upon that, in this series of posts, let's discuss how to validate request data.

When developing a REST API, it's important to validate request data, and in case of invalid data, return a 4xx response with a precise body containing field-wise error details.

Let’s take an example. Say we have a signup form, as below:

public class SignupForm {

    public String email;
    public String name;
    public String password;

// getters and setters
}

And we receive the input in a controller method, as shown below:

@RestController("/users")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public UserResource signup(@RequestBody SignupForm user) {
        return userService.signup(user);
    }
}

The service method that’s being called above could look like below:

@Service
public class UserService {

    public UserResource signup(SignupForm user) {
        // save the user
        // return userResource;
    }
}

So, if a field is blank, how to send an error response?

Using Bean Validation

A good way to validate input data, like the signup form above, is to use bean validation. Spring framework has good support for it. In fact, its Hibernate implementation is auto-configured by Spring Boot.

So, to validate our signup form using bean validation, first annotate its fields with constraint annotations like  @NotBlank, @Email and  @Size:

public class SignupForm {

    @NotBlank
    @Email
    @Size(min=4, max=200)
    public String email;

    @NotBlank
    @Size(min=1, max=100)
    public String name;

    @NotBlank
    @Size(min=6, max=30)
    public String password;

    // getters and setters    
}

Then, annotate the SignupForm parameter in the handler with @Valid, as shown below:

@RestController("/users")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public UserResource signup(@RequestBody @Valid SignupForm user) {
        return userService.signup(user);
    }
}

Now, in case of any errors, Spring would throw an exception instead of executing the controller code. The exception can then be handled in a cross-functional way (as we have discussed in a previous post), and in the next post we'll look at how exactly to do it.

But before that, one thing is worth discussing — some people prefer to do the validation in the service layer. Validation is business logic, after all, and so service layer seems ideal for it. Doing it in the service layer also allows us to do some pre-processing in the controller — see this for example.

So, how to do that? Let's resume in the next post to discuss that and many more real-world stuff, like

  • Why and how to use validation groups

  • How to display custom i18n error messages

  • How to code custom annotations — for primitive fields as well as entire forms

  • Custom validation best practices

So, see you in the next post!

Spring Framework Web Service

Published at DZone with permission of Sanjay Patel. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Key Assumption of Modern Work Culture
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • Connecting Your Devs' Work to the Business
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: