mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Enable validation in AutoConfigureWebMvc
Closes gh-7582
This commit is contained in:
parent
c2ca21bb63
commit
d8d5950e99
@ -45,7 +45,7 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess
|
||||
public class ValidationAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnResource(resources = "META-INF/services/javax.validation.spi.ValidationProvider")
|
||||
@ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider")
|
||||
@Conditional(OnValidatorAvailableCondition.class)
|
||||
@ConditionalOnMissingBean
|
||||
public MethodValidationPostProcessor methodValidationPostProcessor() {
|
||||
|
@ -173,5 +173,15 @@
|
||||
<artifactId>spring-plugin-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-el</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -78,6 +78,7 @@ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
|
||||
|
||||
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.web.servlet;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Example {@link Controller} used with {@link WebMvcTest} tests.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RestController
|
||||
@Validated
|
||||
public class ExampleController3 {
|
||||
|
||||
@GetMapping("/three/{id}")
|
||||
public String three(@PathVariable @Size(max = 4) String id) {
|
||||
return "Hello " + id;
|
||||
}
|
||||
|
||||
}
|
@ -16,13 +16,18 @@
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.web.servlet;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@ -31,11 +36,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
* Tests for {@link WebMvcTest} when no explicit controller is defined.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(secure = false)
|
||||
public class WebMvcTestAllControllersIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@ -57,4 +66,17 @@ public class WebMvcTestAllControllersIntegrationTests {
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunValidationSuccess() throws Exception {
|
||||
this.mvc.perform(get("/three/OK"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Hello OK"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunValidationFailure() throws Exception {
|
||||
this.thrown.expectCause(isA(ConstraintViolationException.class));
|
||||
this.mvc.perform(get("/three/invalid"));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user