How to use POST XML with RestTemplate

I intend to use a POST XML message using the Spring Rest Template. After a series of crashes, I begin to doubt whether the Spring Rest Template POST can send an XML message. This is the leisure client that I developed. RestTemplate is designed to create an XML HTTP message in the RestFul web service:

Class RestClient{ public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); //This JAXB Message converter is intended to marshal an XML message over HTTP. //However, I find this converter is not doing the intended function. Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter(); List<MediaType> mediaTypes = new ArrayList<MediaType>(); mediaTypes.add(MediaType.TEXT_HTML); jaxbMessageConverter.setSupportedMediaTypes(mediaTypes); messageConverters.add(jaxbMessageConverter); restTemplate.setMessageConverters(messageConverters); restTemplate.postForLocation("http://localhost:8080/RecipeProject/restCallConsumer", "<add><somefield></somefield></add>",String.class); } 

}

This controller is designed to use an XML message. The controller was written to verify that the client can send the XML message accordingly:

 @RequestMapping("/") @Controller public class HomeController { @RequestMapping(value = "/restCallConsumer", method = RequestMethod.POST) public String restCallConsumer(String anXML) { System.out.println("anXML: " + anXML); return "aView"; } } 

Most of the examples I have found around using XML with RestTemplate use an object binding tool. This tool maps an object to XML and vice versa. In my case, I only have an XML string that I want to send via an HTTP message. Has anyone done what I'm trying to do? Perhaps the RestFul client is not intended for what I'm trying to do. Any answer would be appreciated :)

EDIT

An XML message is created by serializing the map using Xstream. This is the code that does this:

  com.google.common.collect.LinkedListMultimap.ListMultimap<String, String> multimap = com.google.common.collect.LinkedListMultimap.LinkedListMultimap.create(); multimap.put("x", "1"); multimap.put("x", "2"); multimap.put("y", "3"); XStream xStream = new XStream(new DomDriver()); xStream.registerConverter(new MapEntryConverter(xStream.getMapper())); xStream.alias("add", multimap.getClass()); String xml = xStream.toXML(multimap); System.out.println(xml); 

This code is designed to convert a multimap to an XML string using a converter named MapEntryConverter. This is the code for the converter:

 public static class MapEntryConverter extends MapConverter { public MapEntryConverter(Mapper mapper) { super(mapper); } public boolean canConvert(Class clazz) { return ListMultimap.class.isAssignableFrom(clazz); } public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { ListMultimap<String, String> map = (ListMultimap<String, String>) value; for (String key : map.keys()) { writer.startNode(key); writer.setValue(map.get(key).get(0)); writer.endNode(); } } } 

EDIT

I am changing my code according to the recommended @artbristol. I saw this in the log file:

DEBUG: org.springframework.web.client.RestTemplate - Writing [1 1 3] using [ org.springframework.http.converter.StringHttpMessageConverter@1d 34263a]

It seems restTemplate is an XML message. However, the anXML parameter in the controller is NULL. Does this mean that the XML message could not reach the controller? Maybe the controller is not executed correctly?

+6
source share
1 answer

You do not need to use Spring JAXB marshalling message converter - you have already completed this work by turning it into String . Just POSTing String (as in your code) should work (lose the String.class argument, although intended for URL variables, and get rid of calling setMessageConverters , because this prevents StringHttpMessageConverter from working by default).

+2
source

All Articles