Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -9,3 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
- [Liveness and Readiness Probes in Spring Boot](https://www.baeldung.com/spring-liveness-readiness-probes)
|
||||
- [Custom Information in Spring Boot Info Endpoint](https://www.baeldung.com/spring-boot-info-actuator-custom)
|
||||
- [Health Indicators in Spring Boot](https://www.baeldung.com/spring-boot-health-indicators)
|
||||
|
||||
@@ -10,4 +10,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Create a Custom Auto-Configuration with Spring Boot](https://www.baeldung.com/spring-boot-custom-auto-configuration)
|
||||
- [Guide to ApplicationContextRunner in Spring Boot](https://www.baeldung.com/spring-boot-context-runner)
|
||||
- [A Guide to Spring Boot Configuration Metadata](https://www.baeldung.com/spring-boot-configuration-metadata)
|
||||
- [Display Auto-Configuration Report in Spring Boot](https://www.baeldung.com/spring-boot-auto-configuration-report)
|
||||
- [Display Auto-Configuration Report in Spring Boot](https://www.baeldung.com/spring-boot-auto-configuration-report)
|
||||
- [The Spring @ConditionalOnProperty Annotation](https://www.baeldung.com/spring-conditionalonproperty)
|
||||
|
||||
@@ -14,15 +14,7 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-exceptions</name>
|
||||
<description>Demo project for working with Spring Boot exceptions</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<description>Demo project for working with Spring Boot exceptions</description>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot-exceptions</finalName>
|
||||
@@ -32,68 +24,6 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<delimiters>
|
||||
<delimiter>@</delimiter>
|
||||
</delimiters>
|
||||
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/AutoconfigurationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.intro.App</start-class>
|
||||
<spring-boot.version>2.2.3.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -4,3 +4,4 @@ This module contains articles about Keycloak in Spring Boot projects.
|
||||
|
||||
## Relevant articles:
|
||||
- [A Quick Guide to Using Keycloak with Spring Boot](https://www.baeldung.com/spring-boot-keycloak)
|
||||
- [Custom User Attributes with Keycloak](https://www.baeldung.com/keycloak-custom-user-attributes)
|
||||
|
||||
@@ -6,4 +6,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
||||
|
||||
- [Circular View Path Error](https://www.baeldung.com/spring-circular-view-path-error)
|
||||
- [Download an Image or a File with Spring MVC](https://www.baeldung.com/spring-controller-return-image-file)
|
||||
- [Spring MVC Async vs Spring WebFlux](https://www.baeldung.com/spring-mvc-async-vs-webflux)
|
||||
- More articles: [[prev -->]](/spring-boot-modules/spring-boot-mvc-2)
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
@@ -37,4 +41,4 @@
|
||||
<commons-io.version>2.7</commons-io.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.springvalidation;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
public class ServletInitializer extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(SpringValidationApplication.class);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.springvalidation;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringValidationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringValidationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.springvalidation.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.springvalidation.domain.UserAccount;
|
||||
import com.baeldung.springvalidation.interfaces.BasicInfo;
|
||||
|
||||
@Controller
|
||||
public class UserAccountController {
|
||||
|
||||
@GetMapping("/getUserForm")
|
||||
public String showUserForm(Model theModel) {
|
||||
UserAccount theUser = new UserAccount();
|
||||
theModel.addAttribute("useraccount", theUser);
|
||||
return "userHome";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/saveBasicInfo", method = RequestMethod.POST)
|
||||
public String saveBasicInfo(@Valid @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/saveBasicInfoStep1", method = RequestMethod.POST)
|
||||
public String saveBasicInfoStep1(@Validated(BasicInfo.class) @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.springvalidation.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;
|
||||
|
||||
import com.baeldung.springvalidation.interfaces.AdvanceInfo;
|
||||
import com.baeldung.springvalidation.interfaces.BasicInfo;
|
||||
|
||||
public class UserAccount {
|
||||
|
||||
@NotNull(groups = BasicInfo.class)
|
||||
@Size(min = 4, max = 15, groups = BasicInfo.class)
|
||||
private String password;
|
||||
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String name;
|
||||
|
||||
@Min(value = 18, message = "Age should not be less than 18", groups = AdvanceInfo.class)
|
||||
private int age;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String phone;
|
||||
|
||||
@Valid
|
||||
@NotNull(groups = AdvanceInfo.class)
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.springvalidation.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;
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.springvalidation.interfaces;
|
||||
|
||||
public interface AdvanceInfo {
|
||||
// validation group marker interface
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.springvalidation.interfaces;
|
||||
|
||||
public interface BasicInfo {
|
||||
// validation group marker interface
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.mvc.view.prefix=/WEB-INF/views/
|
||||
spring.mvc.view.suffix=.html
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>SpringValidation</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>Error!!!</h3>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>SpringValidation</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>SUCCESS!!!</h3>
|
||||
</body>
|
||||
</html>
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.springvalidation;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureMockMvc
|
||||
public class UserAccountUnitTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void givenSaveBasicInfo_whenCorrectInput_thenSuccess() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfo")
|
||||
.accept(MediaType.TEXT_HTML)
|
||||
.param("name", "test123")
|
||||
.param("password", "pass"))
|
||||
.andExpect(view().name("success"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSaveBasicInfoStep1_whenCorrectInput_thenSuccess() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfoStep1")
|
||||
.accept(MediaType.TEXT_HTML)
|
||||
.param("name", "test123")
|
||||
.param("password", "pass"))
|
||||
.andExpect(view().name("success"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSaveBasicInfoStep1_whenIncorrectInput_thenError() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfoStep1")
|
||||
.accept(MediaType.TEXT_HTML))
|
||||
// .param("name", "test123")
|
||||
// .param("password", "pass"))
|
||||
.andExpect(model().errorCount(2))
|
||||
// .andExpect(view().name("error"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,4 +6,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Define a Map in YAML for a POJO?](https://www.baeldung.com/yaml-map-pojo)
|
||||
|
||||
- [Using application.yml vs application.properties in Spring Boot](https://www.baeldung.com/spring-boot-yaml-vs-properties)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
## Relevant Articles:
|
||||
|
||||
- [Hiding Endpoints From Swagger Documentation in Spring Boot](https://www.baeldung.com/spring-swagger-hiding-endpoints)
|
||||
Reference in New Issue
Block a user