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

@@ -109,6 +109,22 @@ public class MappingJackson2HttpMessageConverterTests {
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
}
@Test
public void getSupportedMediaTypes() {
MediaType[] defaultMediaTypes = {MediaType.APPLICATION_JSON, MediaType.parseMediaType("application/*+json")};
assertThat(converter.getSupportedMediaTypes()).containsExactly(defaultMediaTypes);
assertThat(converter.getSupportedMediaTypes(MyBean.class)).containsExactly(defaultMediaTypes);
MediaType halJson = MediaType.parseMediaType("application/hal+json");
converter.registerObjectMappersForType(MyBean.class, map -> {
map.put(halJson, new ObjectMapper());
map.put(MediaType.APPLICATION_JSON, new ObjectMapper());
});
assertThat(converter.getSupportedMediaTypes(MyBean.class)).containsExactly(halJson, MediaType.APPLICATION_JSON);
assertThat(converter.getSupportedMediaTypes(Map.class)).containsExactly(defaultMediaTypes);
}
@Test
public void readTyped() throws IOException {
String body = "{" +

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.
@@ -232,12 +232,10 @@ class RestTemplateTests {
void requestAvoidsDuplicateAcceptHeaderValues() throws Exception {
HttpMessageConverter<?> firstConverter = mock(HttpMessageConverter.class);
given(firstConverter.canRead(any(), any())).willReturn(true);
given(firstConverter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(firstConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
HttpMessageConverter<?> secondConverter = mock(HttpMessageConverter.class);
given(secondConverter.canRead(any(), any())).willReturn(true);
given(secondConverter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(secondConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "https://example.com/", requestHeaders);
@@ -651,7 +649,7 @@ class RestTemplateTests {
template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
ParameterizedTypeReference<List<Integer>> intList = new ParameterizedTypeReference<List<Integer>>() {};
given(converter.canRead(intList.getType(), null, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(converter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(converter.canWrite(String.class, String.class, null)).willReturn(true);
HttpHeaders requestHeaders = new HttpHeaders();
@@ -774,8 +772,7 @@ class RestTemplateTests {
private void mockHttpMessageConverter(MediaType mediaType, Class<?> type) {
given(converter.canRead(type, null)).willReturn(true);
given(converter.canRead(type, mediaType)).willReturn(true);
given(converter.getSupportedMediaTypes())
.willReturn(Collections.singletonList(mediaType));
given(converter.getSupportedMediaTypes(type)).willReturn(Collections.singletonList(mediaType));
given(converter.canRead(type, mediaType)).willReturn(true);
given(converter.canWrite(type, null)).willReturn(true);
given(converter.canWrite(type, mediaType)).willReturn(true);