1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .web .servlet .mvc .method .annotation ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .lang .reflect .Type ;
20
21
import java .util .ArrayList ;
21
22
import java .util .Arrays ;
22
23
import java .util .Collections ;
29
30
import org .junit .Test ;
30
31
31
32
import org .springframework .core .MethodParameter ;
33
+ import org .springframework .http .HttpInputMessage ;
32
34
import org .springframework .http .HttpStatus ;
33
35
import org .springframework .http .MediaType ;
34
36
import org .springframework .http .ResponseEntity ;
35
37
import org .springframework .http .converter .HttpMessageConverter ;
38
+ import org .springframework .http .converter .StringHttpMessageConverter ;
36
39
import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
37
40
import org .springframework .http .converter .json .MappingJacksonValue ;
38
41
import org .springframework .http .server .ServerHttpRequest ;
39
42
import org .springframework .http .server .ServerHttpResponse ;
40
43
import org .springframework .http .server .ServletServerHttpResponse ;
44
+ import org .springframework .lang .Nullable ;
41
45
import org .springframework .mock .web .test .MockHttpServletRequest ;
42
46
import org .springframework .mock .web .test .MockHttpServletResponse ;
43
47
import org .springframework .ui .Model ;
60
64
* Unit tests for {@link RequestMappingHandlerAdapter}.
61
65
*
62
66
* @author Rossen Stoyanchev
67
+ * @author Sam Brannen
63
68
* @see ServletAnnotationControllerHandlerMethodTests
64
69
* @see HandlerMethodAnnotationDetectionTests
65
70
* @see RequestMappingHandlerAdapterIntegrationTests
@@ -380,8 +385,15 @@ public void addAttributes(Model model) {
380
385
}
381
386
}
382
387
388
+ /**
389
+ * This class additionally implements {@link RequestBodyAdvice} solely for the purpose
390
+ * of verifying that controller advice implementing both {@link ResponseBodyAdvice}
391
+ * and {@link RequestBodyAdvice} does not get registered twice.
392
+ *
393
+ * @see <a href="https://github.com/spring-projects/spring-framework/pull/22638">gh-22638</a>
394
+ */
383
395
@ ControllerAdvice
384
- private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice {
396
+ private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice implements RequestBodyAdvice {
385
397
386
398
@ SuppressWarnings ("unchecked" )
387
399
@ Override
@@ -396,6 +408,35 @@ protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaT
396
408
map .put ("message" , bodyContainer .getValue ());
397
409
bodyContainer .setValue (map );
398
410
}
411
+
412
+ @ Override
413
+ public boolean supports (MethodParameter methodParameter , Type targetType ,
414
+ Class <? extends HttpMessageConverter <?>> converterType ) {
415
+
416
+ return StringHttpMessageConverter .class .equals (converterType );
417
+ }
418
+
419
+ @ Override
420
+ public HttpInputMessage beforeBodyRead (HttpInputMessage inputMessage , MethodParameter parameter ,
421
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
422
+
423
+ return inputMessage ;
424
+ }
425
+
426
+ @ Override
427
+ public Object afterBodyRead (Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
428
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
429
+
430
+ return body ;
431
+ }
432
+
433
+ @ Override
434
+ public Object handleEmptyBody (@ Nullable Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
435
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
436
+
437
+ return "default value for empty body" ;
438
+ }
439
+
399
440
}
400
441
401
442
@ ControllerAdvice
0 commit comments