org.springframework.http.converter.xml.MarshallingHttpMessageConverter Java Examples

The following examples show how to use org.springframework.http.converter.xml.MarshallingHttpMessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example #2
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #3
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #5
Source File: RestSpringModuleConfig.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #6
Source File: MvcConfig.java    From tutorials with MIT License 5 votes vote down vote up
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
    final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();

    final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
    xmlConverter.setMarshaller(xstreamMarshaller);
    xmlConverter.setUnmarshaller(xstreamMarshaller);

    return xmlConverter;
}