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
@@ -342,8 +347,15 @@ public void addAttributes(Model model) {
342
347
}
343
348
}
344
349
350
+ /**
351
+ * This class additionally implements {@link RequestBodyAdvice} solely for the purpose
352
+ * of verifying that controller advice implementing both {@link ResponseBodyAdvice}
353
+ * and {@link RequestBodyAdvice} does not get registered twice.
354
+ *
355
+ * @see <a href="https://github.com/spring-projects/spring-framework/pull/22638">gh-22638</a>
356
+ */
345
357
@ ControllerAdvice
346
- private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice {
358
+ private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice implements RequestBodyAdvice {
347
359
348
360
@ Override
349
361
protected void beforeBodyWriteInternal (MappingJacksonValue bodyContainer , MediaType contentType ,
@@ -357,6 +369,35 @@ protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaT
357
369
map .put ("message" , bodyContainer .getValue ());
358
370
bodyContainer .setValue (map );
359
371
}
372
+
373
+ @ Override
374
+ public boolean supports (MethodParameter methodParameter , Type targetType ,
375
+ Class <? extends HttpMessageConverter <?>> converterType ) {
376
+
377
+ return StringHttpMessageConverter .class .equals (converterType );
378
+ }
379
+
380
+ @ Override
381
+ public HttpInputMessage beforeBodyRead (HttpInputMessage inputMessage , MethodParameter parameter ,
382
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
383
+
384
+ return inputMessage ;
385
+ }
386
+
387
+ @ Override
388
+ public Object afterBodyRead (Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
389
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
390
+
391
+ return body ;
392
+ }
393
+
394
+ @ Override
395
+ public Object handleEmptyBody (@ Nullable Object body , HttpInputMessage inputMessage , MethodParameter parameter ,
396
+ Type targetType , Class <? extends HttpMessageConverter <?>> converterType ) {
397
+
398
+ return "default value for empty body" ;
399
+ }
400
+
360
401
}
361
402
362
403
}
0 commit comments