Merge pull request #10885 from eugenp/bael-4957-v2
rename project, format
This commit is contained in:
commit
626035e44c
@ -73,6 +73,7 @@
|
||||
<module>spring-boot-actuator</module>
|
||||
<module>spring-boot-data-2</module>
|
||||
<module>spring-boot-react</module>
|
||||
<module>spring-boot-validation</module>
|
||||
</modules>
|
||||
|
||||
<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,21 +5,16 @@
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-boot-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>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@SpringBootApplication
|
||||
public class SpringServiceLayerValidationApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringServiceLayerValidationApp.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringServiceLayerValidationApp.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -11,12 +11,12 @@ import com.baeldung.spring.servicevalidation.service.UserAccountService;
|
||||
@RestController
|
||||
public class UserAccountController {
|
||||
|
||||
@Autowired
|
||||
private UserAccountService service;
|
||||
@Autowired
|
||||
private UserAccountService service;
|
||||
|
||||
@PostMapping("/addUserAccount")
|
||||
public Object addUserAccount(@RequestBody UserAccount userAccount) {
|
||||
return service.addUserAccount(userAccount);
|
||||
}
|
||||
@PostMapping("/addUserAccount")
|
||||
public Object addUserAccount(@RequestBody UserAccount userAccount) {
|
||||
return service.addUserAccount(userAccount);
|
||||
}
|
||||
|
||||
}
|
@ -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,11 +1,12 @@
|
||||
package com.baeldung.spring.servicevalidation.domain;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class UserAddress {
|
||||
|
||||
|
||||
@NotBlank
|
||||
private String countryCode;
|
||||
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
@ -13,5 +14,5 @@ public class UserAddress {
|
||||
public void setCountryCode(String countryCode) {
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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