Polishing

This commit is contained in:
Juergen Hoeller 2017-02-17 21:06:58 +01:00
parent 74bdcd8ed4
commit 1ee0626c94
6 changed files with 26 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -47,14 +47,12 @@ import org.springframework.validation.annotation.Validated;
* at the type level of the containing target class, applying to all public service methods * at the type level of the containing target class, applying to all public service methods
* of that class. By default, JSR-303 will validate against its default group only. * of that class. By default, JSR-303 will validate against its default group only.
* *
* <p>As of Spring 5.0, this functionality requires a Bean Validation 1.1 provider * <p>As of Spring 5.0, this functionality requires a Bean Validation 1.1 provider.
* (such as Hibernate Validator 5.x).
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.1 * @since 3.1
* @see MethodValidationPostProcessor * @see MethodValidationPostProcessor
* @see javax.validation.executable.ExecutableValidator * @see javax.validation.executable.ExecutableValidator
* @see org.hibernate.validator.method.MethodValidator
*/ */
public class MethodValidationInterceptor implements MethodInterceptor { public class MethodValidationInterceptor implements MethodInterceptor {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,14 +48,12 @@ import org.springframework.validation.annotation.Validated;
* inline constraint annotations. Validation groups can be specified through {@code @Validated} * inline constraint annotations. Validation groups can be specified through {@code @Validated}
* as well. By default, JSR-303 will validate against its default group only. * as well. By default, JSR-303 will validate against its default group only.
* *
* <p>As of Spring 4.0, this functionality requires either a Bean Validation 1.1 provider * <p>As of Spring 5.0, this functionality requires a Bean Validation 1.1 provider.
* (such as Hibernate Validator 5.x) or the Bean Validation 1.0 API with Hibernate Validator
* 4.3. The actual provider will be autodetected and automatically adapted.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.1 * @since 3.1
* @see MethodValidationInterceptor * @see MethodValidationInterceptor
* @see org.hibernate.validator.method.MethodValidator * @see javax.validation.executable.ExecutableValidator
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class MethodValidationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor public class MethodValidationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,8 +54,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.1 * @since 3.1
*/ */
public class ModelAttributeMethodProcessor public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -250,7 +250,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
} }
/** /**
* Validate the request part if applicable. * Validate the binding target if applicable.
* <p>The default implementation checks for {@code @javax.validation.Valid}, * <p>The default implementation checks for {@code @javax.validation.Valid},
* Spring's {@link org.springframework.validation.annotation.Validated}, * Spring's {@link org.springframework.validation.annotation.Validated},
* and custom annotations whose name starts with "Valid". * and custom annotations whose name starts with "Valid".

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -110,8 +110,8 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
* @param mavContainer the ModelAndViewContainer for this request * @param mavContainer the ModelAndViewContainer for this request
* @param providedArgs "given" arguments matched by type (not resolved) * @param providedArgs "given" arguments matched by type (not resolved)
*/ */
public void invokeAndHandle(ServletWebRequest webRequest, public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception { Object... providedArgs) throws Exception {
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs); Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
setResponseStatus(webRequest); setResponseStatus(webRequest);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -142,7 +142,6 @@ import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -159,7 +158,9 @@ import static org.junit.Assert.*;
*/ */
public class MvcNamespaceTests { public class MvcNamespaceTests {
public static final String VIEWCONTROLLER_BEAN_NAME = "org.springframework.web.servlet.config.viewControllerHandlerMapping"; public static final String VIEWCONTROLLER_BEAN_NAME =
"org.springframework.web.servlet.config.viewControllerHandlerMapping";
private GenericWebApplicationContext appContext; private GenericWebApplicationContext appContext;
@ -197,7 +198,7 @@ public class MvcNamespaceTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
NativeWebRequest webRequest = new ServletWebRequest(request); NativeWebRequest webRequest = new ServletWebRequest(request);
ContentNegotiationManager manager = mapping.getContentNegotiationManager(); ContentNegotiationManager manager = mapping.getContentNegotiationManager();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest)); assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
assertNotNull(adapter); assertNotNull(adapter);
@ -697,7 +698,8 @@ public class MvcNamespaceTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
NativeWebRequest webRequest = new ServletWebRequest(request); NativeWebRequest webRequest = new ServletWebRequest(request);
assertEquals(Arrays.asList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest)); assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
manager.resolveMediaTypes(webRequest));
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class); ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertNotNull(compositeResolver); assertNotNull(compositeResolver);
@ -882,7 +884,7 @@ public class MvcNamespaceTests {
assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray()); assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
assertNull(config.getExposedHeaders()); assertNull(config.getExposedHeaders());
assertTrue(config.getAllowCredentials()); assertTrue(config.getAllowCredentials());
assertEquals(new Long(1800), config.getMaxAge()); assertEquals(Long.valueOf(1800), config.getMaxAge());
} }
} }
@ -905,14 +907,14 @@ public class MvcNamespaceTests {
assertArrayEquals(new String[]{"header1", "header2", "header3"}, config.getAllowedHeaders().toArray()); assertArrayEquals(new String[]{"header1", "header2", "header3"}, config.getAllowedHeaders().toArray());
assertArrayEquals(new String[]{"header1", "header2"}, config.getExposedHeaders().toArray()); assertArrayEquals(new String[]{"header1", "header2"}, config.getExposedHeaders().toArray());
assertFalse(config.getAllowCredentials()); assertFalse(config.getAllowCredentials());
assertEquals(new Long(123), config.getMaxAge()); assertEquals(Long.valueOf(123), config.getMaxAge());
config = configs.get("/resources/**"); config = configs.get("/resources/**");
assertArrayEquals(new String[]{"http://domain1.com"}, config.getAllowedOrigins().toArray()); assertArrayEquals(new String[]{"http://domain1.com"}, config.getAllowedOrigins().toArray());
assertArrayEquals(new String[]{"GET", "HEAD", "POST"}, config.getAllowedMethods().toArray()); assertArrayEquals(new String[]{"GET", "HEAD", "POST"}, config.getAllowedMethods().toArray());
assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray()); assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
assertNull(config.getExposedHeaders()); assertNull(config.getExposedHeaders());
assertTrue(config.getAllowCredentials()); assertTrue(config.getAllowCredentials());
assertEquals(new Long(1800), config.getMaxAge()); assertEquals(Long.valueOf(1800), config.getMaxAge());
} }
} }
@ -928,21 +930,21 @@ public class MvcNamespaceTests {
@DateTimeFormat(iso = ISO.DATE) @DateTimeFormat(iso = ISO.DATE)
@Target({ElementType.PARAMETER}) @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface IsoDate { public @interface IsoDate {
} }
@NumberFormat(style = NumberFormat.Style.PERCENT) @NumberFormat(style = NumberFormat.Style.PERCENT)
@Target({ElementType.PARAMETER}) @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface PercentNumber { public @interface PercentNumber {
} }
@Validated(MyGroup.class) @Validated(MyGroup.class)
@Target({ElementType.PARAMETER}) @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface MyValid { public @interface MyValid {
} }
@ -961,6 +963,7 @@ public class MvcNamespaceTests {
public void testBind(@RequestParam @IsoDate Date date, public void testBind(@RequestParam @IsoDate Date date,
@RequestParam(required = false) @PercentNumber Double percent, @RequestParam(required = false) @PercentNumber Double percent,
@MyValid TestBean bean, BindingResult result) { @MyValid TestBean bean, BindingResult result) {
this.date = date; this.date = date;
this.percent = percent; this.percent = percent;
this.recordedValidationError = (result.getErrorCount() == 1); this.recordedValidationError = (result.getErrorCount() == 1);