rename project, format
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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!";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user