Merge pull request #4896 from kartiksingla/BAEL-1967

[BAEL-1967] - Custom validation MessageSource in Spring Boot
This commit is contained in:
Loredana Crusoveanu 2018-08-04 23:47:01 +03:00 committed by GitHub
commit 5a5051fe60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 131 additions and 10 deletions

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
@ -27,6 +27,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,20 @@
package com.baeldung.springbootmvc;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springbootmvc.model.LoginForm;
@RestController
@RequestMapping("/")
public class LoginController {
@PostMapping("loginform")
public String processLogin(@Valid LoginForm form) {
return "Success";
}
}

View File

@ -2,12 +2,11 @@ package com.baeldung.springbootmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
public class SpringBootMvcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMvcApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootMvcApplication.class, args);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.springbootmvc.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@Configuration
public class CustomMessageSourceConfiguration {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
}

View File

@ -32,12 +32,13 @@ public class FaviconConfiguration {
requestHandler.setLocations(locations);
return requestHandler;
}
//@Controller
// @Controller
static class FaviconController {
@RequestMapping(value="favicon.ico", method=RequestMethod.GET)
@RequestMapping(value = "favicon.ico", method = RequestMethod.GET)
@ResponseBody
void favicon() {}
void favicon() {
}
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.springbootmvc.model;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class LoginForm {
@NotEmpty(message = "{email.notempty}")
@Email
private String email;
@NotNull
private String password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -0,0 +1 @@
email.notempty=Please provide valid email id.

View File

@ -0,0 +1 @@
email.notempty=Veuillez fournir un identifiant de messagerie valide.

View File

@ -0,0 +1,35 @@
package com.baeldung.springbootmvc;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.baeldung.springbootmvc.config.CustomMessageSourceConfiguration;
@RunWith(SpringRunner.class)
@WebMvcTest(value = LoginController.class, secure = false)
@ContextConfiguration(classes = { SpringBootMvcApplication.class, CustomMessageSourceConfiguration.class })
public class LoginControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void givenLoginForm_whenEmailFieldNotProvided_testCustomValidMessageIsReturned() throws Exception {
RequestBuilder builder = MockMvcRequestBuilders.post("/loginform").param("email", "").param("password", "helo");
// header("accept-language", "fr").
MvcResult perform = mockMvc.perform(builder).andReturn();
Assert.assertTrue(perform.getResolvedException().getMessage().contains("valid email"));
}
}