Challenge: Spring MVC Advanced Concepts

Test your understanding of custom error messages and custom validation by performing exception handling in the given application.

Problem statement

Assume you have an existing Spring application for a Pet Adoption system. Your task is to:

  • Migrate the application from XML-based configuration to Java-based configuration.

  • Add validation to ensure certain fields are required and the pet's age is valid before a new pet is added to the database.

Requirements

To successfully complete this challenge, you need to:

  • Implement the MyDispatcherServletInitializer to configure the DispatcherServlet.

  • Implement the SpringMVCConfig to replace the XML configuration.

  • Add validation annotations to ensure that:

    • ownerName is a required field.

    • age is a positive integer and is a required field.

    • petId follows the specified pattern to only contain uppercase letters, lowercase letters, digits, and hyphens, and it must have at least one of these characters.

  • Implement custom validation logic for age to ensure it is a positive number.

Try it yourself

Implement the necessary classes and configurations in the code widget below to achieve the required functionality.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.