BAEL-4957 - Spring validation in the service layer (#10811)

This commit is contained in:
raviavhad 2021-06-07 16:03:08 +05:30 committed by GitHub
parent 049d9e671f
commit 3db3072b45
9 changed files with 318 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,4 @@
## Spring Service Layer Validation
This module contains articles about validation in Service layer of Spring Boot project.

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring-service-layer-validation</groupId>
<artifactId>spring-service-layer-validation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<String, UserAccount> DB = new HashMap<String, UserAccount>();
public String addUserAccount(UserAccount useraccount) {
DB.put(useraccount.getName(), useraccount);
return "success";
}
public Collection<UserAccount> getAllUserAccounts() {
Collection<UserAccount> list = DB.values();
if(list.isEmpty()) {
list.addAll(DB.values());
}
return list;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<ConstraintViolation<UserAccount>> violations = validator.validate(useraccount);
if (!violations.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (ConstraintViolation<UserAccount> constraintViolation : violations) {
sb.append(constraintViolation.getMessage());
}
dao.addUserAccount(useraccount);
throw new ConstraintViolationException("Error occurred: " + sb.toString(), violations);
}
return "Account for " + useraccount.getName() + " Added!";
}
}