spring integration之http-rest例子解析
spring integration例子代码下载地址,以及如何导入eclipse并使用maven构建等请参看上一篇:
Spring Integration实例代码分析之basic--http
先看一下http-rest例子的readmeThis sample demonstrates how you can send an HTTP request to a Spring Integration's HTTP service while utilizing Spring Integration's new HTTP Path usage;
This sample also uses Spring Security for HTTP Basic authentication. With HTTP Path facility, the client program can send requests with URL Variables.
It consists of two parts - Client and Server.
The following client program can be used to test the HTTP Path usage.
1. RestHttpClientTest. It uses Spring's RestTemplate to assemble and send HTTP request
Server is Spring Integration's HTTP endpoint configuration.
此示例演示了如何发送一个HTTP请求到一个Spring集成的HTTP服务,同时利用Spring Integration的新的HTTP Path;
此示例还使用了Spring HTTP基本身份验证安全性。 HTTP Path,客户端程序可以发送请求的URL变量。
它由两部分组成 - 客户端和服务器。
客户端程序,可以用来测试使用HTTP path。RestHttpClientTest。它使用Spring的RestTemplate的组装和发送HTTP请求
服务端是Spring集成的HTTP端点配置。
运行run as --> run on server -->选择tomcat运行用junit运行RestHttpClientTest,就可以得到数据。@Testpublic void testGetEmployeeAsJson() throws Exception{Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search?format=json";HttpHeaders headers = getHttpHeadersWithUserCredentials(new HttpHeaders());headers.add("Accept", "application/json");HttpEntity<Object> request = new HttpEntity<Object>(headers);ResponseEntity<?> httpResponse = restTemplate.exchange(fullUrl, HttpMethod.GET, request, EmployeeList.class, employeeSearchMap);logger.info("Return Status :"+httpResponse.getHeaders().get("X-Return-Status"));logger.info("Return Status Message :"+httpResponse.getHeaders().get("X-Return-Status-Msg"));assertTrue(httpResponse.getStatusCode().equals(HttpStatus.OK));jaxbJacksonObjectMapper.writeValue(System.out, httpResponse.getBody());}
可以获取到xml内容,也可以得到json内容,上面为测试类的主要代码。