Media types by Class for HttpMessageConverter

See gh-26212
This commit is contained in:
Rossen Stoyanchev
2021-02-04 16:10:35 +00:00
parent 1721b0b8d7
commit f4c9f6b860
13 changed files with 164 additions and 86 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -138,16 +138,15 @@ public class HttpEntityMethodProcessorMockTests {
public void setup() throws Exception {
stringHttpMessageConverter = mock(HttpMessageConverter.class);
given(stringHttpMessageConverter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
resourceMessageConverter = mock(HttpMessageConverter.class);
given(resourceMessageConverter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
resourceRegionMessageConverter = mock(HttpMessageConverter.class);
given(resourceRegionMessageConverter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(MediaType.ALL));
given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
processor = new HttpEntityMethodProcessor(Arrays.asList(
stringHttpMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
@@ -241,7 +240,7 @@ public class HttpEntityMethodProcessorMockTests {
servletRequest.setMethod("POST");
servletRequest.addHeader("Content-Type", contentType.toString());
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(contentType));
given(stringHttpMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(contentType));
given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(false);
assertThatExceptionOfType(HttpMediaTypeNotSupportedException.class).isThrownBy(() ->
@@ -314,7 +313,7 @@ public class HttpEntityMethodProcessorMockTests {
servletRequest.addHeader("Accept", accepted.toString());
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes())
given(stringHttpMessageConverter.getSupportedMediaTypes(any()))
.willReturn(Collections.singletonList(TEXT_PLAIN));
assertThatExceptionOfType(HttpMediaTypeNotAcceptableException.class).isThrownBy(() ->
@@ -328,7 +327,7 @@ public class HttpEntityMethodProcessorMockTests {
.body("<foo/>");
given(stringHttpMessageConverter.canWrite(String.class, TEXT_PLAIN)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(TEXT_PLAIN));
assertThatThrownBy(() ->
processor.handleReturnValue(
@@ -345,7 +344,7 @@ public class HttpEntityMethodProcessorMockTests {
ResponseEntity<String> returnValue = ResponseEntity.ok().body("<foo/>");
given(stringHttpMessageConverter.canWrite(String.class, TEXT_PLAIN)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(TEXT_PLAIN));
assertThatThrownBy(() ->
processor.handleReturnValue(
@@ -362,7 +361,7 @@ public class HttpEntityMethodProcessorMockTests {
servletRequest.addHeader("Accept", accepted.toString());
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes())
given(stringHttpMessageConverter.getSupportedMediaTypes(any()))
.willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.canWrite(String.class, accepted)).willReturn(false);
@@ -575,7 +574,7 @@ public class HttpEntityMethodProcessorMockTests {
.ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.canWrite(ByteArrayResource.class, APPLICATION_OCTET_STREAM)).willReturn(true);
processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest);
@@ -735,6 +734,7 @@ public class HttpEntityMethodProcessorMockTests {
private void initStringMessageConversion(MediaType accepted) {
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.getSupportedMediaTypes(String.class)).willReturn(Collections.singletonList(TEXT_PLAIN));
given(stringHttpMessageConverter.canWrite(String.class, accepted)).willReturn(true);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -113,10 +113,13 @@ public class RequestResponseBodyMethodProcessorMockTests {
public void setup() throws Exception {
stringMessageConverter = mock(HttpMessageConverter.class);
given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(stringMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
resourceMessageConverter = mock(HttpMessageConverter.class);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
resourceRegionMessageConverter = mock(HttpMessageConverter.class);
given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
processor = new RequestResponseBodyMethodProcessor(
Arrays.asList(stringMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
@@ -388,7 +391,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
servletRequest.addHeader("Accept", accepted);
given(stringMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringMessageConverter.getSupportedMediaTypes()).willReturn(supported);
given(stringMessageConverter.getSupportedMediaTypes(any())).willReturn(supported);
given(stringMessageConverter.canWrite(String.class, accepted)).willReturn(true);
processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ import java.util.List;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -85,6 +87,9 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
@SuppressWarnings("unused")
public class RequestResponseBodyMethodProcessorTests {
protected static final String NEWLINE_SYSTEM_PROPERTY = System.getProperty("line.separator");
private ModelAndViewContainer container;
private MockHttpServletRequest servletRequest;
@@ -358,9 +363,37 @@ public class RequestResponseBodyMethodProcessorTests {
assertThat(this.servletResponse.getHeader("Content-Type")).isEqualTo("image/jpeg");
}
// SPR-13135
@Test // gh-26212
public void handleReturnValueWithObjectMapperByTypeRegistration() throws Exception {
MediaType halFormsMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
MediaType halMediaType = MediaType.parseMediaType("application/hal+json");
@Test
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.registerObjectMappersForType(SimpleBean.class, map -> map.put(halMediaType, objectMapper));
this.servletRequest.addHeader("Accept", halFormsMediaType + "," + halMediaType);
SimpleBean simpleBean = new SimpleBean();
simpleBean.setId(12L);
simpleBean.setName("Jason");
RequestResponseBodyMethodProcessor processor =
new RequestResponseBodyMethodProcessor(Collections.singletonList(converter));
MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("getSimpleBean"), -1);
processor.writeWithMessageConverters(simpleBean, returnType, this.request);
assertThat(this.servletResponse.getHeader("Content-Type")).isEqualTo(halMediaType.toString());
assertThat(this.servletResponse.getContentAsString()).isEqualTo(
"{" + NEWLINE_SYSTEM_PROPERTY +
" \"id\" : 12," + NEWLINE_SYSTEM_PROPERTY +
" \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY +
"}");
}
@Test // SPR-13135
public void handleReturnValueWithInvalidReturnType() throws Exception {
Method method = getClass().getDeclaredMethod("handleAndReturnOutputStream");
MethodParameter returnType = new MethodParameter(method, -1);
@@ -778,6 +811,10 @@ public class RequestResponseBodyMethodProcessorTests {
return null;
}
SimpleBean getSimpleBean() {
return null;
}
private static abstract class MyParameterizedController<DTO extends Identifiable> {

View File

@@ -92,7 +92,6 @@ import org.springframework.http.HttpOutputMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -961,7 +960,9 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
void unsupportedRequestBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
@@ -972,7 +973,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
assertThat(response.getHeader("Accept")).as("No Accept response header set").isNotNull();
assertThat(response.getHeader("Accept")).isEqualTo("text/plain");
}
@PathPatternsParameterizedTest