rename project, format
This commit is contained in:
parent
3cabed6990
commit
f777c86fcf
@ -73,6 +73,7 @@
|
|||||||
<module>spring-boot-actuator</module>
|
<module>spring-boot-actuator</module>
|
||||||
<module>spring-boot-data-2</module>
|
<module>spring-boot-data-2</module>
|
||||||
<module>spring-boot-react</module>
|
<module>spring-boot-react</module>
|
||||||
|
<module>spring-boot-validation</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
## Spring Service Layer Validation
|
|
||||||
|
|
||||||
This module contains articles about validation in Service layer of Spring Boot project.
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
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!";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
2
spring-boot-modules/spring-boot-validation/README.md
Normal file
2
spring-boot-modules/spring-boot-validation/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
## Relevant Articles
|
||||||
|
|
@ -5,20 +5,15 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.baeldung.spring-service-layer-validation</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-service-layer-validation</artifactId>
|
<artifactId>spring-boot-validation</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<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>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<version>2.4.5</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
@ -0,0 +1,31 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.spring.servicevalidation.domain;
|
package com.baeldung.spring.servicevalidation.domain;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
public class UserAddress {
|
public class UserAddress {
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
@ -0,0 +1,42 @@
|
|||||||
|
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!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import com.baeldung.spring.servicevalidation.SpringServiceLayerValidationApp;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = SpringServiceLayerValidationApp.class)
|
||||||
|
public class SpringContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user