Extra ObjectMapper registrations in HttpMessageConverter

See gh-26212
This commit is contained in:
Rossen Stoyanchev
2021-02-01 11:50:52 +00:00
parent 836976d732
commit 1721b0b8d7
2 changed files with 135 additions and 20 deletions

View File

@@ -73,6 +73,27 @@ public class MappingJackson2HttpMessageConverterTests {
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isTrue();
}
@Test
public void canReadWithObjectMapperRegistrationForType() {
MediaType halJsonMediaType = MediaType.parseMediaType("application/hal+json");
MediaType halFormsJsonMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
assertThat(converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
assertThat(converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
assertThat(converter.canRead(MyBean.class, halFormsJsonMediaType)).isTrue();
assertThat(converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
converter.registerObjectMappersForType(MyBean.class, map -> {
map.put(halJsonMediaType, new ObjectMapper());
map.put(MediaType.APPLICATION_JSON, new ObjectMapper());
});
assertThat(converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
assertThat(converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
assertThat(converter.canRead(MyBean.class, halFormsJsonMediaType)).isFalse();
assertThat(converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
}
@Test
public void canWrite() {
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json"))).isTrue();