Java Code Examples for org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter

The following examples show how to use org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter. These examples are extracted from open source projects. 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
@Test // SPR-16172
public void httpEntityWithContentType() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		messageConverters.add(new MappingJackson2HttpMessageConverter());
		messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
		adapterDef.getPropertyValues().add("messageConverters", messageConverters);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, ResponseEntityController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-entity");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("application/xml", response.getHeader("Content-Type"));
	assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
					"<testEntity><name>Foo Bar</name></testEntity>", response.getContentAsString());
}
 
Example 2
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	List<Person> performers = Arrays.asList(
			new Person("Vladimir Ashkenazy").setSomeBoolean(false),
			new Person("Yehudi Menuhin").setSomeBoolean(true));

	this.people = new PeopleWrapper(composers, performers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 3
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	this.people = new PeopleWrapper(composers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 4
@Test // SPR-16172
public void httpEntityWithContentType() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		messageConverters.add(new MappingJackson2HttpMessageConverter());
		messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
		adapterDef.getPropertyValues().add("messageConverters", messageConverters);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, ResponseEntityController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-entity");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("application/xml", response.getHeader("Content-Type"));
	assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
					"<testEntity><name>Foo Bar</name></testEntity>", response.getContentAsString());
}
 
Example 5
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	List<Person> performers = Arrays.asList(
			new Person("Vladimir Ashkenazy").setSomeBoolean(false),
			new Person("Yehudi Menuhin").setSomeBoolean(true));

	this.people = new PeopleWrapper(composers, performers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 6
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	this.people = new PeopleWrapper(composers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 7
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<Source>());

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}
}
 
Example 8
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<Source>());

	if (jaxb2Present && !jackson2Present) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}
}
 
Example 9
@Before
public void setup() {

	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	List<Person> performers = Arrays.asList(
			new Person("Vladimir Ashkenazy").setSomeBoolean(false),
			new Person("Yehudi Menuhin").setSomeBoolean(true));

	this.people = new PeopleWrapper(composers, performers);

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 10
@Before
public void setup() {

	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	this.people = new PeopleWrapper(composers);

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example 11
Source Project: riptide   Source File: ExecuteTest.java    License: MIT License 6 votes vote down vote up
@Test
void shouldFailIfNoConverterFoundForBodyOfUnknownContentType() {
    final MockSetup setup = new MockSetup("https://api.example.com", Collections.emptyList());
    final MockRestServiceServer server = setup.getServer();
    final Http unit = setup.getHttpBuilder()
            .converter(new Jaxb2RootElementHttpMessageConverter()).build();

    // we never actually make the request, but the mock server is doing some magic pre-actively
    server.expect(requestTo(url))
            .andRespond(withSuccess());

    final CompletionException exception = assertThrows(CompletionException.class, () ->
            unit.delete(url)
                    .body(new Success(true))
                    .dispatch(contentType())
                    .join());

    assertThat(exception.getCause(), is(instanceOf(RestClientException.class)));
    assertThat(exception.getMessage(), containsString("no suitable HttpMessageConverter found"));
    assertThat(exception.getMessage(), containsString("org.zalando.riptide.model.Success"));
}
 
Example 12
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<>());

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	/*else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}*/

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	/*if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}*/
}
 
Example 13
public AllEncompassingFormHttpMessageConverter() {
	try {
		addPartConverter(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}
}
 
Example 14
public AllEncompassingFormHttpMessageConverter() {
	try {
		addPartConverter(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}
}
 
Example 15
Source Project: lams   Source File: RestTemplate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example 16
Source Project: spring4-understanding   Source File: RestTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example 17
@Override
protected void initRestTemplate(RestTemplate restTemplate) {
    super.initRestTemplate(restTemplate);
    restTemplate.setErrorHandler(new DCTMJaxbErrorHandler(restTemplate.getMessageConverters()));
    for(HttpMessageConverter<?> c : restTemplate.getMessageConverters()) {
        if(c instanceof FormHttpMessageConverter) {
            ((FormHttpMessageConverter)c).addPartConverter(new Jaxb2RootElementHttpMessageConverter());
            break;
        }
    }
}
 
Example 18
Source Project: sdk-rest   Source File: RestTemplateFactory.java    License: MIT License 5 votes vote down vote up
private static RestTemplate createRestTemplate() {
	List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
	messageConverters.add(new ByteArrayHttpMessageConverter());
	messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
	messageConverters.add(new ResourceHttpMessageConverter());
	messageConverters.add(new SourceHttpMessageConverter<Source>());
	messageConverters.add(new AllEncompassingFormHttpMessageConverter());
	messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	messageConverters.add(new MappingJackson2HttpMessageConverter());

	return new RestTemplate(messageConverters);
}
 
Example 19
private List<HttpMessageConverter<?>> converters() {
  List<HttpMessageConverter<?>> result = Lists.newArrayList();
  result.add(new ByteArrayHttpMessageConverter());
  result.add(new StringHttpMessageConverter());
  result.add(new ResourceHttpMessageConverter());
  result.add(new SourceHttpMessageConverter<>());
  result.add(new AllEncompassingFormHttpMessageConverter());
  result.add(new Jaxb2RootElementHttpMessageConverter());
  result.add(gsonHttpMessageConverter);
  return result;
}
 
Example 20
Source Project: spring-analysis-note   Source File: RestTemplate.java    License: MIT License 4 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter HttpMessageConverters} are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter(false));
	try {
		this.messageConverters.add(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		this.messageConverters.add(new JsonbHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
	}
	if (jackson2CborPresent) {
		this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
	}

	this.uriTemplateHandler = initUriTemplateHandler();
}
 
Example 21
Source Project: java-technology-stack   Source File: RestTemplate.java    License: MIT License 4 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter HttpMessageConverters} are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter(false));
	try {
		this.messageConverters.add(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		this.messageConverters.add(new JsonbHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
	}
	if (jackson2CborPresent) {
		this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
	}

	this.uriTemplateHandler = initUriTemplateHandler();
}
 
Example 22
Source Project: Microservices-with-Spring-Cloud   Source File: WebConfig.java    License: MIT License 4 votes vote down vote up
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.removeIf(c -> c instanceof Jaxb2RootElementHttpMessageConverter);
}
 
Example 23
Source Project: Microservices-with-Spring-Cloud   Source File: WebConfig.java    License: MIT License 4 votes vote down vote up
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.removeIf(c -> c instanceof Jaxb2RootElementHttpMessageConverter);
}
 
Example 24
Source Project: Microservices-with-Spring-Cloud   Source File: WebConfig.java    License: MIT License 4 votes vote down vote up
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.removeIf(c -> c instanceof Jaxb2RootElementHttpMessageConverter);
}
 
Example 25
/**
 * Adds a set of default HttpMessageConverter instances to the given list.
 * Subclasses can call this method from {@link #configureMessageConverters(List)}.
 * @param messageConverters the list to add the default message converters to
 */
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
	StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
	stringConverter.setWriteAcceptCharset(false);

	messageConverters.add(new ByteArrayHttpMessageConverter());
	messageConverters.add(stringConverter);
	messageConverters.add(new ResourceHttpMessageConverter());
	messageConverters.add(new SourceHttpMessageConverter<Source>());
	messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		messageConverters.add(new AtomFeedHttpMessageConverter());
		messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		messageConverters.add(new MappingJackson2XmlHttpMessageConverter(
				Jackson2ObjectMapperBuilder.xml().applicationContext(this.applicationContext).build()));
	}
	else if (jaxb2Present) {
		messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		messageConverters.add(new MappingJackson2HttpMessageConverter(
				Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build()));
	}
	else if (gsonPresent) {
		messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example 26
Source Project: riptide   Source File: HttpMessageConvertersOverrideTest.java    License: MIT License 4 votes vote down vote up
@Bean
@Qualifier("github-invalid")
public ClientHttpMessageConverters githubInvalidHttpMessageConverters() {
    return new ClientHttpMessageConverters(singletonList(new Jaxb2RootElementHttpMessageConverter()));
}
 
Example 27
Source Project: riptide   Source File: HttpMessageConvertersOverrideTest.java    License: MIT License 4 votes vote down vote up
@Test
void shouldOverride() {
    assertThat(unit.getConverters(), contains(instanceOf(Jaxb2RootElementHttpMessageConverter.class)));
}
 
Example 28
Source Project: moserp   Source File: TestRestConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
private void filterXmlConverters(RestTemplate restTemplate) {
    Stream<HttpMessageConverter<?>> messageConverterStream = restTemplate.getMessageConverters().stream();
    Predicate<HttpMessageConverter<?>> noXmlConverters = httpMessageConverter -> !(httpMessageConverter instanceof Jaxb2RootElementHttpMessageConverter) && !(httpMessageConverter instanceof MappingJackson2XmlHttpMessageConverter);
    List<HttpMessageConverter<?>> filtered = messageConverterStream.filter(noXmlConverters).collect(Collectors.toList());
    restTemplate.setMessageConverters(filtered);
}