From 3db3072b45a94ae9249b1dcbaf875f796877696a Mon Sep 17 00:00:00 2001 From: raviavhad Date: Mon, 7 Jun 2021 16:03:08 +0530 Subject: [PATCH] BAEL-4957 - Spring validation in the service layer (#10811) --- .../.gitignore | 63 +++++++++++++++ .../README.md | 4 + .../pom.xml | 46 +++++++++++ .../SpringServiceLayerValidationApp.java | 13 ++++ .../controller/UserAccountController.java | 22 ++++++ .../servicevalidation/dao/UserAccountDao.java | 32 ++++++++ .../servicevalidation/domain/UserAccount.java | 77 +++++++++++++++++++ .../servicevalidation/domain/UserAddress.java | 17 ++++ .../service/UserAccountService.java | 44 +++++++++++ 9 files changed, 318 insertions(+) create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/.gitignore create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/README.md create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/pom.xml create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/SpringServiceLayerValidationApp.java create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/controller/UserAccountController.java create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/dao/UserAccountDao.java create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAccount.java create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAddress.java create mode 100644 spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/service/UserAccountService.java diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/.gitignore b/spring-boot-modules/spring-boot-servicelayer-validation/.gitignore new file mode 100644 index 0000000000..8f5ee06047 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/.gitignore @@ -0,0 +1,63 @@ +# Created by https://www.gitignore.io/api/eclipse + +### Eclipse ### + +.metadata +target/ +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +### Eclipse Patch ### +# Eclipse Core +.project + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# End of https://www.gitignore.io/api/eclipse \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/README.md b/spring-boot-modules/spring-boot-servicelayer-validation/README.md new file mode 100644 index 0000000000..7c39135db6 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/README.md @@ -0,0 +1,4 @@ +## Spring Service Layer Validation + +This module contains articles about validation in Service layer of Spring Boot project. + diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/pom.xml b/spring-boot-modules/spring-boot-servicelayer-validation/pom.xml new file mode 100644 index 0000000000..31ec757699 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/pom.xml @@ -0,0 +1,46 @@ + + + + 4.0.0 + + com.baeldung.spring-service-layer-validation + spring-service-layer-validation + 0.0.1-SNAPSHOT + + + UTF-8 + 12 + 12 + + + + org.springframework.boot + spring-boot-starter-parent + 2.4.5 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.hibernate.validator + hibernate-validator + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/SpringServiceLayerValidationApp.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/SpringServiceLayerValidationApp.java new file mode 100644 index 0000000000..ea80a5da33 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/SpringServiceLayerValidationApp.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.servicevalidation; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringServiceLayerValidationApp { + + public static void main(String[] args) { + SpringApplication.run(SpringServiceLayerValidationApp.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/controller/UserAccountController.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/controller/UserAccountController.java new file mode 100644 index 0000000000..b558ed89c2 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/controller/UserAccountController.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.servicevalidation.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.spring.servicevalidation.domain.UserAccount; +import com.baeldung.spring.servicevalidation.service.UserAccountService; + +@RestController +public class UserAccountController { + + @Autowired + private UserAccountService service; + + @PostMapping("/addUserAccount") + public Object addUserAccount(@RequestBody UserAccount userAccount) { + return service.addUserAccount(userAccount); + } + +} diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/dao/UserAccountDao.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/dao/UserAccountDao.java new file mode 100644 index 0000000000..d170ceef58 --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/dao/UserAccountDao.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.servicevalidation.dao; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.stereotype.Service; + +import com.baeldung.spring.servicevalidation.domain.UserAccount; + +@Service +public class UserAccountDao { + + private Map DB = new HashMap(); + + public String addUserAccount(UserAccount useraccount) { + DB.put(useraccount.getName(), useraccount); + return "success"; + } + + public Collection getAllUserAccounts() { + + Collection list = DB.values(); + if(list.isEmpty()) { + list.addAll(DB.values()); + } + return list; + + } + + +} diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAccount.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAccount.java new file mode 100644 index 0000000000..5b0e795a8a --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAccount.java @@ -0,0 +1,77 @@ +package com.baeldung.spring.servicevalidation.domain; + +import javax.validation.Valid; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +public class UserAccount { + + @NotNull(message = "Password must be between 4 to 15 characters") + @Size(min = 4, max = 15) + private String password; + + @NotBlank(message = "Name must not be blank") + private String name; + + @Min(value = 18, message = "Age should not be less than 18") + private int age; + + @NotBlank(message = "Phone must not be blank") + private String phone; + + @Valid + @NotNull(message = "UserAddress must not be blank") + private UserAddress useraddress; + + public UserAddress getUseraddress() { + return useraddress; + } + + public void setUseraddress(UserAddress useraddress) { + this.useraddress = useraddress; + } + + public UserAccount() { + + } + + public UserAccount(String email, String password, String name, int age) { + this.password = password; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + +} diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAddress.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAddress.java new file mode 100644 index 0000000000..85e7dfc05c --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/domain/UserAddress.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.servicevalidation.domain; + +import javax.validation.constraints.NotBlank; +public class UserAddress { + + @NotBlank + private String countryCode; + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + +} diff --git a/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/service/UserAccountService.java b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/service/UserAccountService.java new file mode 100644 index 0000000000..9a376fda6b --- /dev/null +++ b/spring-boot-modules/spring-boot-servicelayer-validation/src/main/java/com/baeldung/spring/servicevalidation/service/UserAccountService.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.servicevalidation.service; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validator; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.spring.servicevalidation.dao.UserAccountDao; +import com.baeldung.spring.servicevalidation.domain.UserAccount; + +@Service +public class UserAccountService { + + @Autowired + private Validator validator; + + @Autowired + private UserAccountDao dao; + + public String addUserAccount(UserAccount useraccount) { + + Set> violations = validator.validate(useraccount); + + if (!violations.isEmpty()) { + StringBuilder sb = new StringBuilder(); + for (ConstraintViolation constraintViolation : violations) { + sb.append(constraintViolation.getMessage()); + } + + dao.addUserAccount(useraccount); + + throw new ConstraintViolationException("Error occurred: " + sb.toString(), violations); + } + + + + return "Account for " + useraccount.getName() + " Added!"; + } + +}