java 如何使用 RestTemplate POST XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15333149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to POST XML using RestTemplate
提问by zfranciscus
I intend to POST an XML message using Spring Rest Template. After a number of failures, I am starting to doubt whether Spring Rest Template can POST an XML message. This is a Restful client that I developed. The RestTemplate is intended to do an HTTP post of an XML to a RestFul webservice:
我打算使用 Spring Rest Template 发布一条 XML 消息。在多次失败后,我开始怀疑 Spring Rest Template 是否可以发布 XML 消息。这是我开发的一个 Restful 客户端。RestTemplate 旨在将 XML 的 HTTP 发布到 RestFul 网络服务:
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 intended to consume the XML message. The controller was written to test that the client can send the XML message appropriately:
此控制器旨在使用 XML 消息。编写控制器是为了测试客户端是否可以适当地发送 XML 消息:
@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 example I found around using XML with RestTemplate involves using an object mapping tool. This tool maps object to an XML and vice versa. In my case, I only have an XML string that I want to send via HTTP post. Has anyone accomplished what I am trying to do ? It could be that RestFul client is not intended for what I am trying to do. Any answer would be appreciated :)
我发现的大部分将 XML 与 RestTemplate 结合使用的示例都涉及使用对象映射工具。此工具将对象映射到 XML,反之亦然。就我而言,我只有一个要通过 HTTP 发布的 XML 字符串。有没有人完成我想要做的事情?可能是 RestFul 客户端不适用于我想要做的事情。任何答案将不胜感激:)
EDIT
编辑
THe XML message is produced by serializing a Map using Xstream. This is the code that does that:
XML 消息是通过使用 Xstream 序列化 Map 生成的。这是执行此操作的代码:
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 intended to convert the multimap into an XML string using a converter named MapEntryConverter. This is the code for the Converter:
此代码旨在使用名为 MapEntryConverter 的转换器将多图转换为 XML 字符串。这是转换器的代码:
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 change my code as per @artbristol recommended. I saw this in the log file:
我按照@artbristol 的建议更改了我的代码。我在日志文件中看到了这一点:
DEBUG: org.springframework.web.client.RestTemplate - Writing [ 1 1 3 ] using [org.springframework.http.converter.StringHttpMessageConverter@1d34263a]
调试:org.springframework.web.client.RestTemplate - 使用 [org.springframework.http.converter.StringHttpMessageConverter@1d34263a] 编写 [1 1 3]
It looks like the restTemplate is POST-ing the XML message. However, the anXML parameter in the controller is null. Does this mean that the XML message could not reach the controller ? Could it be that the controller is not implemented correctly ?
看起来 restTemplate 正在发布 XML 消息。但是,控制器中的 anXML 参数为空。这是否意味着 XML 消息无法到达控制器?可能是控制器没有正确实现?
采纳答案by artbristol
You don't need to use Spring's JAXB marshalling message converter - you've already done the work by making it into a String
. Just POSTing the String
(like in your code) ought to work (lose the String.class
argument though, that's intended for URL variables, and get rid of the setMessageConverters
call, because that is preventing the default StringHttpMessageConverter
from working).
您不需要使用 Spring 的 JAXB 编组消息转换器 - 您已经通过将其转换为String
. 只是发布String
(就像在您的代码中一样)应该可以工作(String.class
尽管丢失了参数,这是用于 URL 变量的,并摆脱setMessageConverters
调用,因为这会阻止默认值StringHttpMessageConverter
工作)。