/** * @param xstreamMarshaller The fully configured {@link XStreamMarshaller} * @return The configured {@link MarshallingHttpMessageConverter}. */ @Bean public MarshallingHttpMessageConverter getMessageConverter( final XStreamMarshaller xstreamMarshaller) { final List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_XML); mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); xmlConverter.setSupportedMediaTypes(mediaTypes); xmlConverter.setMarshaller(xstreamMarshaller); xmlConverter.setUnmarshaller(xstreamMarshaller); return xmlConverter; }
@Test public void responseBodyArgMismatch() throws ServletException, IOException { initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { @Override public void initialize(GenericWebApplicationContext 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()); }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FilteringJackson2HttpMessageConverter jsonConverter = new FilteringJackson2HttpMessageConverter(); jsonConverter.setSupportedMediaTypes(ApiMediaTypes.getJsonMediaTypes()); converters.add(jsonConverter); MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); xmlConverter.setSupportedMediaTypes(ApiMediaTypes.getXmlMediaTypes()); XStreamMarshaller xStreamMarshaller = new XStreamMarshaller(); xmlConverter.setMarshaller(xStreamMarshaller); xmlConverter.setUnmarshaller(xStreamMarshaller); converters.add(xmlConverter); FilteringTextMessageConverter filteringTextMessageConverter = new FilteringTextMessageConverter(new MediaType("text", "plain", Charset.forName("utf-8"))); filteringTextMessageConverter.setDelimiter("\t"); converters.add(filteringTextMessageConverter); }
@Test public void responseBodyArgMismatch() throws ServletException, IOException { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(RequestBodyArgMismatchController.class)); 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(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", messageConverter); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); 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(); servlet.service(request, response); assertEquals(400, response.getStatus()); }
private HttpMessageConverter<Object> createXmlHttpMessageConverter() { MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); XStreamMarshaller xstreamMarshaller = new XStreamMarshaller(); xmlConverter.setMarshaller(xstreamMarshaller); xmlConverter.setUnmarshaller(xstreamMarshaller); return xmlConverter; }
/** * 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()); }