如何使用Spring RestTemplate发送XML POST请求?
是否可以使用spring
发送XML
POST
请求,例如RestTemplate
?
我想将以下xml发送到url localhost:8080/xml/availability
123
我还想动态地在每个请求上添加自定义http标头(!)。
我怎么能用弹簧实现这个目标?
首先,定义您的HTTP
标头,如下所示:
HttpHeaders headers = new HttpHeaders(); headers.add("header_name", "header_value");
您可以使用此方法设置任何HTTP
标头。 对于众所周知的标头,您可以使用预定义的方法。 例如,要设置Content-Type
标头:
headers.setContentType(MediaType.APPLICATION_XML);
然后定义HttpEntity
或RequestEntity
来准备您的请求对象:
HttpEntity request = new HttpEntity (body, headers);
如果您以某种方式访问XML
字符串,则可以使用HttpEntity
。 否则,您可以定义与该XML
对应的POJO。 最后使用postFor...
方法发送请求:
ResponseEntity response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);
在这里,我将请求发送到http://localhost:8080/xml/availability
端点,并将HTTP
响应主体转换为String
。
注意,在上面的例子中, new HttpEntity
可以使用JDK7及更高版本替换为 new HttpEntity<>(...)
。
下面是一个完整的示例,说明如何使用RestTemplate
交换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 = "response
"; String xmlString = "123 "; 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("response
", MediaType.TEXT_HTML)); HttpHeaders headers = new HttpHeaders(); headers.add("header", "value"); HttpEntity request = new HttpEntity<>(document, headers); final ResponseEntity response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class); assertThat(response.getBody(), is(htmlString)); mockServer.verify(); } }
以下查找例如使用RestTemplate将XML作为String交换并接收响应:
String xmlString = "123 "; RestTemplate restTemplate = new RestTemplate(); //Create a list for the message converters List> messageConverters = new ArrayList>(); //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 request = new HttpEntity (xmlString, headers); final ResponseEntity response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);