是否可以使用XML
发送POST
spring
个请求,例如RestTemplate
?
我想将以下xml发送到网址localhost:8080/xml/availability
<AvailReq>
<hotelid>123</hotelid>
</AvailReq>
我还想动态地在每个请求上添加自定义http标头(!)。
我怎么能用春天来实现这个目标?
答案 0 :(得分:26)
首先,定义您的HTTP
标题,如下所示:
HttpHeaders headers = new HttpHeaders();
headers.add("header_name", "header_value");
您可以使用此方法设置任何HTTP
标头。对于众所周知的标头,您可以使用预定义的方法。例如,为了设置Content-Type
标题:
headers.setContentType(MediaType.APPLICATION_XML);
然后定义HttpEntity
或RequestEntity
以准备您的请求对象:
HttpEntity<String> request = new HttpEntity<String>(body, headers);
如果您以某种方式访问XML
字符串,则可以使用HttpEntity<String>
。否则,您可以定义与XML
对应的POJO。最后使用postFor...
方法发送请求:
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/xml/availability", request, String.class);
此处我{{}}将请求POST
发送到http://localhost:8080/xml/availability
端点并将HTTP
响应正文转换为String
。
请注意,在上面的示例中,new HttpEntity<String>(...)
可以使用JDK7及更高版本be replaced with new HttpEntity<>(...)
。
答案 1 :(得分:4)
下面是一个完整的示例,说明如何使用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 = "<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();
}
}
答案 2 :(得分:2)
以下为例,使用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);