Browse Source

Merge branch '5.1.x'

pull/22689/head
Sam Brannen 6 years ago
parent
commit
2a94cb2fe9
  1. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
  2. 45
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

@ -598,10 +598,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
if (!binderMethods.isEmpty()) { if (!binderMethods.isEmpty()) {
this.initBinderAdviceCache.put(adviceBean, binderMethods); this.initBinderAdviceCache.put(adviceBean, binderMethods);
} }
if (RequestBodyAdvice.class.isAssignableFrom(beanType)) { if (RequestBodyAdvice.class.isAssignableFrom(beanType) || ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
requestResponseBodyAdviceBeans.add(adviceBean);
}
if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
requestResponseBodyAdviceBeans.add(adviceBean); requestResponseBodyAdviceBeans.add(adviceBean);
} }
} }

45
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 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.
@ -17,6 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation; package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -29,15 +30,18 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonValue; import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse; import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -60,6 +64,7 @@ import static org.junit.Assert.*;
* Unit tests for {@link RequestMappingHandlerAdapter}. * Unit tests for {@link RequestMappingHandlerAdapter}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @see ServletAnnotationControllerHandlerMethodTests * @see ServletAnnotationControllerHandlerMethodTests
* @see HandlerMethodAnnotationDetectionTests * @see HandlerMethodAnnotationDetectionTests
* @see RequestMappingHandlerAdapterIntegrationTests * @see RequestMappingHandlerAdapterIntegrationTests
@ -342,8 +347,15 @@ public class RequestMappingHandlerAdapterTests {
} }
} }
/**
* This class additionally implements {@link RequestBodyAdvice} solely for the purpose
* of verifying that controller advice implementing both {@link ResponseBodyAdvice}
* and {@link RequestBodyAdvice} does not get registered twice.
*
* @see <a href="https://github.com/spring-projects/spring-framework/pull/22638">gh-22638</a>
*/
@ControllerAdvice @ControllerAdvice
private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice { private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice implements RequestBodyAdvice {
@Override @Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
@ -357,6 +369,35 @@ public class RequestMappingHandlerAdapterTests {
map.put("message", bodyContainer.getValue()); map.put("message", bodyContainer.getValue());
bodyContainer.setValue(map); bodyContainer.setValue(map);
} }
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return StringHttpMessageConverter.class.equals(converterType);
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return inputMessage;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
@Override
public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return "default value for empty body";
}
} }
} }

Loading…
Cancel
Save