/** * Constructor with converters and {@code Request~} and {@code ResponseBodyAdvice}. * @since 4.2 */ public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) { Assert.notEmpty(converters, "'messageConverters' must not be empty"); this.messageConverters = converters; this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters); this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice); }
/** * Constructor with converters and {@code Request~} and {@code ResponseBodyAdvice}. * @since 4.2 */ public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) { Assert.notEmpty(converters, "'messageConverters' must not be empty"); this.messageConverters = converters; this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters); this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice); }
@Test public void controllerAdviceNotApplicable() { Object adviceBean = new ControllerAdviceBean(new TargetedControllerAdvice()); RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean)); String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType, this.converterType, this.request, this.response); assertEquals(this.body, actual); }
@Test public void controllerAdvice() { Object adviceBean = new ControllerAdviceBean(new MyControllerAdvice()); RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean)); String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType, this.converterType, this.request, this.response); assertEquals("body-MyControllerAdvice", actual); }
@SuppressWarnings("unchecked") @Test public void responseBodyAdvice() { RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class); ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class); List<Object> advice = Arrays.asList(requestAdvice, responseAdvice); RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice); String expected = "body++"; given(responseAdvice.supports(this.returnType, this.converterType)).willReturn(true); given(responseAdvice.beforeBodyWrite(eq(this.body), eq(this.returnType), eq(this.contentType), eq(this.converterType), same(this.request), same(this.response))).willReturn(expected); String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType, this.converterType, this.request, this.response); assertEquals(expected, actual); }
@SuppressWarnings("unchecked") @Test public void requestBodyAdvice() throws IOException { RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class); ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class); List<Object> advice = Arrays.asList(requestAdvice, responseAdvice); RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice); HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest()); given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true); given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped); assertSame(wrapped, chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType)); String modified = "body++"; given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified); assertEquals(modified, chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType)); }
/** * Constructor with converters and {@code Request~} and {@code ResponseBodyAdvice}. * @since 4.2 */ public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice) { Assert.notEmpty(converters, "'messageConverters' must not be empty"); this.messageConverters = converters; this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters); this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice); }