@Test public void canWrite() { Marshaller marshaller = mock(Marshaller.class); given(marshaller.supports(Integer.class)).willReturn(false); given(marshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setMarshaller(marshaller); assertFalse(converter.canWrite(Boolean.class, MediaType.TEXT_PLAIN)); assertFalse(converter.canWrite(Integer.class, MediaType.TEXT_XML)); assertTrue(converter.canWrite(String.class, MediaType.TEXT_XML)); }
@Test public void canRead() { Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.supports(Integer.class)).willReturn(false); given(unmarshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); assertFalse(converter.canRead(Boolean.class, MediaType.TEXT_PLAIN)); assertFalse(converter.canRead(Integer.class, MediaType.TEXT_XML)); assertTrue(converter.canRead(String.class, MediaType.TEXT_XML)); }
@Test public void read() throws Exception { String body = "<root>Hello World</root>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); String result = (String) converter.read(Object.class, inputMessage); assertEquals("Invalid result", body, result); }
@Test(expected = UnsupportedOperationException.class) public void supports() throws Exception { new MarshallingHttpMessageConverter().supports(Object.class); } }
@Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz)); }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
@Test public void responseBodyArgMismatch() throws Exception { initServlet(wac -> { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(A.class, B.class); try { marshaller.afterPropertiesSet(); } catch (Exception ex) { throw new BeanCreationException(ex.getMessage(), ex); } MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller); RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", messageConverter); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestBodyArgMismatchController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "<b/>"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "application/xml; charset=utf-8"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(400, response.getStatus()); }
@Test public void write() throws Exception { String body = "<root>Hello World</root>"; MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Marshaller marshaller = mock(Marshaller.class); willDoNothing().given(marshaller).marshal(eq(body), isA(Result.class)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); }
@Test public void readWithTypeMismatchException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); try { converter.read(String.class, inputMessage); fail("Should have thrown HttpMessageNotReadableException"); } catch (HttpMessageNotReadableException ex) { assertTrue(ex.getCause() instanceof TypeMismatchException); } }
@Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz)); }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
/** * Gets a new marshalling HTTP message converter that is aware of our custom JAXB marshaller. * * @return the newly created message converter. */ @Bean public MarshallingHttpMessageConverter marshallingMessageConverter() { // Return a new marshalling HTTP message converter with our custom JAXB marshaller. return new MarshallingHttpMessageConverter(jaxb2Marshaller(), jaxb2Marshaller()); }
@Test public void writeWithMarshallingFailureException() throws Exception { String body = "<root>Hello World</root>"; MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MarshallingFailureException ex = new MarshallingFailureException("forced"); Marshaller marshaller = mock(Marshaller.class); willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.class)); try { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); fail("HttpMessageNotWritableException should be thrown"); } catch (HttpMessageNotWritableException e) { assertTrue("Invalid exception hierarchy", e.getCause() == ex); } }
@Test public void readWithMarshallingFailureException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); try { converter.read(Object.class, inputMessage); fail("HttpMessageNotReadableException should be thrown"); } catch (HttpMessageNotReadableException e) { assertTrue("Invalid exception hierarchy", e.getCause() == ex); } }
@Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz)); }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
/** * Gets a new marshalling HTTP message converter that is aware of our custom JAXB marshaller. * * @return the newly created message converter. */ @Bean public MarshallingHttpMessageConverter marshallingMessageConverter() { // Return a new marshalling HTTP message converter with our custom JAXB marshaller. return new MarshallingHttpMessageConverter(jaxb2Marshaller(), jaxb2Marshaller()); }
@Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return (canWrite(mediaType) && this.marshaller != null && this.marshaller.supports(clazz)); }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
/** * * @param restTemplate REST Template for making HTTP calls * @param bioPAXIOHandler BioPAXIOHandler for reading BioPAX Models */ public PathwayCommons2Client(RestTemplate restTemplate, BioPAXIOHandler bioPAXIOHandler) { this.restTemplate = restTemplate; List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<HttpMessageConverter<?>>(); httpMessageConverters.add(new BioPAXHttpMessageConverter(bioPAXIOHandler)); Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); jaxb2Marshaller.setClassesToBeBound(Help.class, SearchResponseType.class, ErrorType.class); httpMessageConverters.add(new MarshallingHttpMessageConverter(jaxb2Marshaller, jaxb2Marshaller)); restTemplate.setMessageConverters(httpMessageConverters); }