/** * Protected constructor that sets the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} * to {@code text/xml} and {@code application/xml}, and {@code application/*-xml}. */ protected AbstractXmlHttpMessageConverter() { super(MediaType.APPLICATION_XML, MediaType.TEXT_XML, new MediaType("application", "*+xml")); }
/** * Construct a new {@code MappingJackson2SmileHttpMessageConverter} with a custom {@link ObjectMapper} * (must be configured with a {@code SmileFactory} instance). * You can use {@link Jackson2ObjectMapperBuilder} to build it easily. * @see Jackson2ObjectMapperBuilder#smile() */ public MappingJackson2SmileHttpMessageConverter(ObjectMapper objectMapper) { super(objectMapper, new MediaType("application", "x-jackson-smile")); Assert.isInstanceOf(SmileFactory.class, objectMapper.getFactory(), "SmileFactory required"); }
/** * Re-create the given mime type as a media type. * @since 5.0 */ public static MediaType asMediaType(MimeType mimeType) { if (mimeType instanceof MediaType) { return (MediaType) mimeType; } return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); }
@Test(expected = HttpMessageNotReadableException.class) public void readInvalidJson() throws IOException { String body = "FooBar"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); converter.read(MyBean.class, inputMessage); }
@Test public void readValidJsonWithUnknownProperty() throws IOException { String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); converter.read(MyBean.class, inputMessage); // Assert no HttpMessageNotReadableException is thrown }
@Test public void read() throws IOException { Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class); byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream()); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg")); BufferedImage result = converter.read(BufferedImage.class, inputMessage); assertEquals("Invalid height", 500, result.getHeight()); assertEquals("Invalid width", 750, result.getWidth()); }
@Test public void canWrite() { assertTrue("Image not supported", converter.canWrite(BufferedImage.class, null)); assertTrue("Image not supported", converter.canWrite(BufferedImage.class, new MediaType("image", "png"))); assertTrue("Image not supported", converter.canWrite(BufferedImage.class, new MediaType("*", "*"))); }
@Test public void contentType() { MediaType contentType = new MediaType("text", "html", StandardCharsets.UTF_8); headers.setContentType(contentType); assertEquals("Invalid Content-Type header", contentType, headers.getContentType()); assertEquals("Invalid Content-Type header", "text/html;charset=UTF-8", headers.getFirst("Content-Type")); }
/** * Introduced to dispel claims in a thread on Stack Overflow: * <a href="http://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a> */ @Test public void getMimeTypeWithCustomConfiguredType() { sc.addMimeType("enigma", new MediaType("text", "enigma")); assertEquals("text/enigma", sc.getMimeType("filename.enigma")); }
@Test public void useNegotiatedMediaTypeCharset() { MediaType negotiatedMediaType = new MediaType("text", "html", ISO_8859_1); HttpMessageWriter<String> writer = getWriter(TEXT_PLAIN_UTF_8, TEXT_HTML); writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS); assertEquals(negotiatedMediaType, this.response.getHeaders().getContentType()); assertEquals(negotiatedMediaType, this.mediaTypeCaptor.getValue()); }
@Test public void canRead() { assertTrue("Image not supported", converter.canRead(BufferedImage.class, null)); assertTrue("Image not supported", converter.canRead(BufferedImage.class, new MediaType("image", "png"))); }
@Test public void writeOtherCharset() throws IOException, SAXException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); String encoding = "ISO-8859-1"; feed.setEncoding(encoding); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(feed, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "atom+xml", Charset.forName(encoding)), outputMessage.getHeaders().getContentType()); }
@Test public void cantRead() { assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), new MediaType("foo", "bar"))); assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), null)); }
@Test public void writeUTF16() throws IOException { MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, contentType, outputMessage); assertEquals("Invalid result", body, outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)); assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType()); }