1
1
/*
2
- * Copyright 2002-2015 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 ;
61
64
* Unit tests for {@link RequestMappingHandlerAdapter}.
62
65
*
63
66
* @author Rossen Stoyanchev
67
+ * @author Sam Brannen
64
68
* @see ServletAnnotationControllerHandlerMethodTests
65
69
* @see HandlerMethodAnnotationDetectionTests
66
70
* @see RequestMappingHandlerAdapterIntegrationTests
@@ -382,8 +386,15 @@ public void addAttributes(Model model) {
382
386
}
383
387
}
384
388
389
+ /**
390
+ * This class additionally implements {@link RequestBodyAdvice} solely for the purpose
391
+ * of verifying that controller advice implementing both {@link ResponseBodyAdvice}
392
+ * and {@link RequestBodyAdvice} does not get registered twice.
393
+ *
394
+ * @see <a href="https://github.com/spring-projects/spring-framework/pull/22638">gh-22638</a>
395
+ */
385
396
@ ControllerAdvice
386
- private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice {
397
+ private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice implements RequestBodyAdvice {
387
398
388
399
@ SuppressWarnings ("unchecked" )
389
400
@ Override
@@ -398,6 +409,35 @@ protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaT
398
409
map .put ("message" , bodyContainer .getValue ());
399
410
bodyContainer .setValue (map );
400
411
}
412
+
413
+ @ Override
414
+ public boolean supports (MethodParameter methodParameter , Type targetType ,
415
+ Class <? extends HttpMessageConverter <?>> converterType ) {
416
+
417
+ return StringHttpMessageConverter .class .equals (converterType );
418
+ }
419
+
420
+ @ Override
421
+ public HttpInputMessage beforeBodyRead (HttpInputMessage inputMessage , MethodParameter parameter ,
422
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
423
+
424
+ return inputMessage ;
425
+ }
426
+
427
+ @ Override
428
+ public Object afterBodyRead (Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
429
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
430
+
431
+ return body ;
432
+ }
433
+
434
+ @ Override
435
+ public Object handleEmptyBody (Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
436
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
437
+
438
+ return "default value for empty body" ;
439
+ }
440
+
401
441
}
402
442
403
443
@ ControllerAdvice
0 commit comments