Java 如何使用 Spring RestTemplate 发送 XML POST 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35461148/
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 send XML POST requests with Spring RestTemplate?
提问by membersound
Is it possible to send XML
POST
requests with spring
, eg RestTemplate
?
是否可以发送XML
POST
请求spring
,例如RestTemplate
?
I want to send the following xml to the url localhost:8080/xml/availability
我想将以下 xml 发送到 url localhost:8080/xml/availability
<AvailReq>
<hotelid>123</hotelid>
</AvailReq>
Also do I want to add custom http headers on each request dynamically(!).
我还想在每个请求上动态添加自定义 http 标头吗(!)。
How could I achieve this with spring?
我怎么能用春天实现这一点?
采纳答案by Ali Dehghani
First of all, define your HTTP
headers, like following:
首先,定义您的HTTP
标题,如下所示:
HttpHeaders headers = new HttpHeaders();
headers.add("header_name", "header_value");
You can set any HTTP
header with this approach. For well known headers you can use pre-defined methods. For example, in order to set Content-Type
header:
您可以HTTP
使用这种方法设置任何标题。对于众所周知的标头,您可以使用预定义的方法。例如,为了设置Content-Type
标题:
headers.setContentType(MediaType.APPLICATION_XML);
Then define a HttpEntity
or RequestEntity
to prepare your request object:
然后定义一个HttpEntity
orRequestEntity
来准备你的请求对象:
HttpEntity<String> request = new HttpEntity<String>(body, headers);
If you somehow have access to the XML
string, you can use HttpEntity<String>
. Otherwise you can define a POJO corresponding to that XML
. and finally send the request using postFor...
methods:
如果您以某种方式可以访问该XML
字符串,则可以使用HttpEntity<String>
. 否则你可以定义一个对应的 POJO XML
。最后使用postFor...
方法发送请求:
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);
Here i'm POST
ing the request to the http://localhost:8080/xml/availability
endpoint and converting the HTTP
response body into a String
.
在这里,我POST
将请求发送到http://localhost:8080/xml/availability
端点并将HTTP
响应正文转换为String
.
Note, that in the above examples new HttpEntity<String>(...)
can be replaced withnew HttpEntity<>(...)
using JDK7 and later.
请注意,在上面的示例中,new HttpEntity<String>(...)
可以使用 JDK7 及更高版本替换new HttpEntity<>(...)
。
回答by Kamill Sokol
Below you find a complete example how to use a RestTemplate
to exchange XML documents and receive a HTML response:
下面是一个完整的示例,如何使用 aRestTemplate
来交换 XML 文档并接收 HTML 响应:
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class XmlTest {
@Test
public void test() throws Exception {
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
String htmlString = "<p>response</p>";
String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AvailReq><hotelid>123</hotelid></AvailReq>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));
mockServer.expect(requestTo("http://localhost:8080/xml/availability"))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(is(xmlString)))
.andExpect(header("header", "value"))
.andRespond(withSuccess("<p>response</p>", MediaType.TEXT_HTML));
HttpHeaders headers = new HttpHeaders();
headers.add("header", "value");
HttpEntity<Document> request = new HttpEntity<>(document, headers);
final ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);
assertThat(response.getBody(), is(htmlString));
mockServer.verify();
}
}
回答by Sam
Find below for example to use a RestTemplate to exchange XML as String and receive a response:
例如,在下面找到使用 RestTemplate 将 XML 交换为 String 并接收响应的示例:
String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AvailReq><hotelid>123</hotelid></AvailReq>";
RestTemplate restTemplate = new RestTemplate();
//Create a list for the message converters
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
//Add the String Message converter
messageConverters.add(new StringHttpMessageConverter());
//Add the message converters to the restTemplate
restTemplate.setMessageConverters(messageConverters);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<String> request = new HttpEntity<String>(xmlString, headers);
final ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);