restful xml

1.简介

这篇文章的目的是使用Spring Integration HTTP入站适配器实现HTTP Restful API。 本教程分为两个部分:

  • XML配置示例(同一篇文章)。
  • Java DSL示例。 这将在本教程的下一部分中进行说明,展示如何使用Spring Integration Java DSL配置应用程序,并提供Java 7和Java 8的示例。

在查看代码之前,让我们看一下下图,该图显示了应用程序公开的不同服务:

GET操作由HTTP入站网关处理,而其余操作(PUT,POST和DELETE)由HTTP入站通道适配器处理,因为没有响应主体发送回客户端。 以下各节将说明每个操作:

  1. 介绍
  2. 应用配置
  3. 进行操作
  4. 放置和发布操作
  5. 删除操作
  6. 结论

源代码可从Github获得 。

2.应用程序配置

web.xml文件包含分派器Servlet的定义:

<servlet><servlet-name>springServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:xpadro/spring/integration/configuration/http-inbound-config.xml</param-value></init-param>
</servlet>
<servlet-mapping><servlet-name>springServlet</servlet-name><url-pattern>/spring/*</url-pattern>
</servlet-mapping>

以下各节将说明http-inbound-config.xml文件。

下面是pom.xml文件的详细信息。 重要的是要注意杰克逊库。 由于我们将使用JSON表示资源,因此这些库必须存在于类路径中。 否则,框架将不会注册所需的转换器。

<properties><spring-version>4.1.3.RELEASE</spring-version><spring-integration-version>4.1.0.RELEASE</spring-integration-version><slf4j-version>1.7.5</slf4j-version><junit-version>4.9</junit-version><jackson-version>2.3.0</jackson-version>
</properties><dependencies><!-- Spring Framework - Core --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency><!-- Spring Framework - Integration --><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-core</artifactId><version>${spring-integration-version}</version></dependency><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-http</artifactId><version>${spring-integration-version}</version></dependency><!-- JSON --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson-version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson-version}</version></dependency><!-- Testing --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit-version}</version><scope>test</scope></dependency><!-- Logging --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j-version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j-version}</version></dependency>
</dependencies>

3.进行操作

该流程的配置如下所示:

http-inbound-config.xml

网关接收到以下路径的请求:/ persons / {personId}。 请求到达后,将创建一条消息并将其发送到httpGetChannel通道。 然后,网关将等待服务激活器 (personEndpoint)返回响应:

现在,需要解释一些要点:

  • 支持的方法 :此属性指示网关支持哪些方法(仅GET请求)。
  • payload-expression :我们在这里所做的是从URI模板中的personId变量获取值并将其放入消息的有效负载中。 例如,请求路径“ / persons / 3”将成为一条值为“ 3”的消息作为其有效负载。
  • request-mapping :我们可以包含此元素以指定几个属性,并过滤哪些请求将映射到网关。 在示例中,此网关仅处理包含Content-Type标头(consumes属性)和Accept标头(produces属性)的值“ application / json”的请求。

将请求映射到此网关后,便会构建一条消息并将其发送到服务激活器。 在示例中,我们定义了一个简单的bean,它将从服务中获取所需的信息:

@Component
public class PersonEndpoint {private static final String STATUSCODE_HEADER = "http_statusCode";@Autowiredprivate PersonService service;public Message<?> get(Message<String> msg) {long id = Long.valueOf(msg.getPayload());ServerPerson person = service.getPerson(id);if (person == null) {return MessageBuilder.fromMessage(msg).copyHeadersIfAbsent(msg.getHeaders()).setHeader(STATUSCODE_HEADER, HttpStatus.NOT_FOUND).build(); }return MessageBuilder.withPayload(person).copyHeadersIfAbsent(msg.getHeaders()).setHeader(STATUSCODE_HEADER, HttpStatus.OK).build();}//Other operations
}

根据从服务收到的响应,我们将返回被请求的人员或指示未找到人员的状态代码。

现在,我们将测试一切是否按预期进行。 首先,我们定义将响应转换为的ClientPerson类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ClientPerson implements Serializable {private static final long serialVersionUID = 1L;@JsonProperty("id")private int myId;private String name;public ClientPerson() {}public ClientPerson(int id, String name) {this.myId = id;this.name = name;}//Getters and setters
}

然后我们执行测试。 在buildHeaders方法中,我们指定了Accept和Content-Type标头。 请记住,我们在这些标头中使用'application / json'值限制了请求。

@RunWith(BlockJUnit4ClassRunner.class)
public class GetOperationsTest {private static final String URL = "http://localhost:8081/int-http-xml/spring/persons/{personId}";private final RestTemplate restTemplate = new RestTemplate();private HttpHeaders buildHeaders() {HttpHeaders headers = new HttpHeaders();headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));headers.setContentType(MediaType.APPLICATION_JSON); return headers;}@Testpublic void getResource_responseIsConvertedToPerson() {HttpEntity<Integer> entity = new HttpEntity<>(buildHeaders());ResponseEntity<ClientPerson> response = restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 1);assertEquals("John" , response.getBody().getName());assertEquals(HttpStatus.OK, response.getStatusCode());}@Testpublic void getResource_responseIsReceivedAsJson() {HttpEntity<Integer> entity = new HttpEntity<>(buildHeaders());ResponseEntity<String> response = restTemplate.exchange(URL, HttpMethod.GET, entity, String.class, 1);assertEquals("{\"id\":1,\"name\":\"John\",\"age\":25}", response.getBody());assertEquals(HttpStatus.OK, response.getStatusCode());}@Test(expected=HttpClientErrorException.class)public void getResource_sendXml_415errorReturned() {HttpHeaders headers = new HttpHeaders();headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));headers.setContentType(MediaType.APPLICATION_XML);HttpEntity<Integer> entity = new HttpEntity<>(headers);restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 1);}@Test(expected=HttpClientErrorException.class)public void getResource_expectXml_receiveJson_406errorReturned() {HttpHeaders headers = new HttpHeaders();headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));headers.setContentType(MediaType.APPLICATION_JSON);HttpEntity<Integer> entity = new HttpEntity<>(headers);restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 1);}@Test(expected=HttpClientErrorException.class)public void getResource_resourceNotFound_404errorReturned() {HttpEntity<Integer> entity = new HttpEntity<>(buildHeaders());restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 8);}
}

在Content-Type标头中未指定正确的值将导致415不支持的媒体类型错误,因为网关不支持此媒体类型。

另一方面,在Accept标头中指定不正确的值将导致406 Not Acceptable错误,因为网关返回的内容类型不同于预期。

4.放置和发布操作

对于PUT和POST操作,我们使用相同的HTTP入站通道适配器,并利用了为它定义多个路径和方法的可能性。 一旦请求到达,路由器将负责将消息传递到正确的端点。

http-inbound-config.xml

<int-http:inbound-channel-adapter channel="routeRequest" status-code-expression="T(org.springframework.http.HttpStatus).NO_CONTENT"supported-methods="POST, PUT" path="/persons, /persons/{personId}"request-payload-type="xpadro.spring.integration.server.model.ServerPerson"><int-http:request-mapping consumes="application/json"/>
</int-http:inbound-channel-adapter><int:router input-channel="routeRequest" expression="headers.http_requestMethod"><int:mapping value="PUT" channel="httpPutChannel"/><int:mapping value="POST" channel="httpPostChannel"/>
</int:router><int:service-activator ref="personEndpoint" method="put" input-channel="httpPutChannel"/>
<int:service-activator ref="personEndpoint" method="post" input-channel="httpPostChannel"/>

此通道适配器包括两个新属性:

  • status-code-expression :默认情况下,通道适配器确认已收到请求,并返回200状态码。 如果要覆盖此行为,可以在此属性中指定其他状态代码。 在这里,我们指定这些操作将返回204 No Content状态代码。
  • request-payload-type :此属性指定将请求主体转换为哪个类。 如果我们没有定义它,它将无法转换为服务激活器期望的类(ServerPerson)。

收到请求后,适配器会将其发送到路由器期望它的routeRequest通道。 该路由器将检查消息头,并根据“ http_requestMethod”头的值将其传递到适当的端点。

PUT和POST操作都由同一个bean处理:

@Component
public class PersonEndpoint {@Autowiredprivate PersonService service;//Get operationpublic void put(Message<ServerPerson> msg) {service.updatePerson(msg.getPayload());}public void post(Message<ServerPerson> msg) {service.insertPerson(msg.getPayload());}
}

返回类型为空,因为没有期望的响应; 入站适配器将处理状态码的返回。

PutOperationsTest验证是否返回了正确的状态代码以及资源是否已更新:

@RunWith(BlockJUnit4ClassRunner.class)
public class PutOperationsTest {private static final String URL = "http://localhost:8081/int-http-xml/spring/persons/{personId}";private final RestTemplate restTemplate = new RestTemplate();//build headers method@Testpublic void updateResource_noContentStatusCodeReturned() {HttpEntity<Integer> getEntity = new HttpEntity<>(buildHeaders());ResponseEntity<ClientPerson> response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4);ClientPerson person = response.getBody();person.setName("Sandra");HttpEntity<ClientPerson> putEntity = new HttpEntity<ClientPerson>(person, buildHeaders());response = restTemplate.exchange(URL, HttpMethod.PUT, putEntity, ClientPerson.class, 4);assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4);person = response.getBody();assertEquals("Sandra", person.getName());}
}

PostOperationsTest验证是否已添加新资源:

@RunWith(BlockJUnit4ClassRunner.class)
public class PostOperationsTest {private static final String POST_URL = "http://localhost:8081/int-http-xml/spring/persons";private static final String GET_URL = "http://localhost:8081/int-http-xml/spring/persons/{personId}";private final RestTemplate restTemplate = new RestTemplate();//build headers method@Testpublic void addResource_noContentStatusCodeReturned() {ClientPerson person = new ClientPerson(9, "Jana");HttpEntity<ClientPerson> entity = new HttpEntity<ClientPerson>(person, buildHeaders());ResponseEntity<ClientPerson> response = restTemplate.exchange(POST_URL, HttpMethod.POST, entity, ClientPerson.class);assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());HttpEntity<Integer> getEntity = new HttpEntity<>(buildHeaders());response = restTemplate.exchange(GET_URL, HttpMethod.GET, getEntity, ClientPerson.class, 9);person = response.getBody();assertEquals("Jana", person.getName());}
}

5.删除操作

我们的RESTful API的最后一个操作是删除操作。 这次我们为此使用一个单通道适配器:

<int-http:inbound-channel-adapter channel="httpDeleteChannel" status-code-expression="T(org.springframework.http.HttpStatus).NO_CONTENT"supported-methods="DELETE" path="/persons/{personId}" payload-expression="#pathVariables.personId"><int-http:request-mapping consumes="application/json"/>
</int-http:inbound-channel-adapter><int:service-activator ref="personEndpoint" method="delete" input-channel="httpDeleteChannel"/>

通道适配器使我们可以定义返回状态代码,并且我们正在使用有效负载表达式属性将请求的personId映射到消息正文。 该配置与先前操作中的配置略有不同,但是这里没有任何未解释的内容。

服务激活者,我们的人员端点,将请求人员服务删除此资源。

public void delete(Message<String> msg) {long id = Long.valueOf(msg.getPayload());service.deletePerson(id);
}

最后,所需的测试:

@RunWith(BlockJUnit4ClassRunner.class)
public class DeleteOperationsTest {private static final String URL = "http://localhost:8081/int-http-xml/spring/persons/{personId}";private final RestTemplate restTemplate = new RestTemplate();//build headers method@Testpublic void deleteResource_noContentStatusCodeReturned() {HttpEntity<Integer> entity = new HttpEntity<>(buildHeaders());ResponseEntity<ClientPerson> response = restTemplate.exchange(URL, HttpMethod.DELETE, entity, ClientPerson.class, 3);assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());try {response = restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 3);Assert.fail("404 error expected");} catch (HttpClientErrorException e) {assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());}}
}

六,结论

这篇文章是对我们的应用程序的介绍,目的是从已知的角度(xml配置)了解它的结构。 在本教程的下一部分中,我们将使用Java DSL来实现相同的应用程序。 该应用程序将配置为可以在Java 8上运行,但是当使用lambda时,我还将展示如何在Java 7上完成它。

我正在Google Plus和Twitter上发布我的新帖子。 如果您要更新新内容,请关注我。

翻译自: https://www.javacodegeeks.com/2014/12/exposing-http-restful-api-with-inbound-adapters-part-1-xml.html

restful xml

restful xml_使用入站适配器公开HTTP Restful API。 第1部分(XML)相关推荐

  1. 使用入站适配器公开HTTP Restful API。 第1部分(XML)

    1.简介 这篇文章的目的是使用Spring Integration HTTP入站适配器实现HTTP Restful API. 本教程分为两个部分: XML配置示例(同一篇文章). Java DSL示例 ...

  2. php获取搜索框的函数,php获取搜索引擎入站关键词的函数

    本文介绍下,一段可以获取搜索引擎入站关键词的代码,有需要的朋友参考下. 代码如下: 0) { $start=stripos($url,'&'); $s_s_keyword=substr($ur ...

  3. Win10修改防火墙入站规则

    2019独角兽企业重金招聘Python工程师标准>>> 1.如果被Win10强迫更新,早晚更新成怎么也关不掉防火墙的状态.如何设置入站规则呢?首先通过搜索框找到控制面板. 2.选择W ...

  4. 设置计算机的出站和入站规则

    使用快捷键(win+x)打开控制面板: 选择"Windows防火墙" 选择"高级设置" 选择出站或者入站右键进行新增.完成配置 转载于:https://www. ...

  5. Netty核心组件 ChannelPipeline和ChannelHandler与ChannelHandler的入站出站规则

    概述 Netty中ChannelPipeline与Channel的对应关系是一一对应,也就是每个Channel中有且仅有一个ChannelPipeline,可以通过Channel获取唯一的Channe ...

  6. 【kafka】kafka 2.3 关于控制Broker端入站连接数的讨论

    1.概述 本文是 https://www.cnblogs.com/huxi2b/p/11262995.html 的补充 Kafka Broker端处理请求采用Reactor模型.每台Broker上有个 ...

  7. Netty工作笔记0073---Neety的出站和入站机制

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 道理是这样的,如果数据进入客户端是入站, 出客户端 是出站,然后对于服务器来说也是一样的. 入站到 ...

  8. nettry 入站事件如何传递到下一个handler

    nettry 入站事件如何传递到下一个handler : https://blog.csdn.net/yexin94822739/article/details/73338356 转载于:https: ...

  9. 测试路由器的防火墙配置,wan:入站数据,出站数据,转发

    一.测试环境: 联想笔记本电脑,8口千兆海康威视交换机,网件R6220路由器(刷潘多拉固件,重置到默认配置): 将电脑网口连接到路由器lan口,将路由器wan口连到家庭网关(电信光猫,已拨号,192. ...

最新文章

  1. 不用车载传感器,也能L4级自动驾驶?清华百度联手发布全球首个纯路侧感知自动驾驶方案...
  2. Windows SharePoint Services 3.0 Step By Step翻译
  3. 代码重构之没有理由拒绝Lambda表达式
  4. 论管理员的不作为!!!
  5. java heap space flex_Flash builder 4内存优化之java heap space解决办法
  6. ASP.NET Core使用功能开关控制路由访问
  7. 网络爬虫--15.【糗事百科实战】多线程实现
  8. 智能驾驶系统是怎样看懂交通标志的?3张流程图给你讲明白
  9. Linux学习总结(36)——创建、复制、剪切、重命名、清空和删除文件夹的命令
  10. 如何聊才能突出自己软实力,打动面试官
  11. Python是Python的web框架
  12. Spring整合ActiveMQ之嵌入(二)
  13. 西门子触摸屏数据历史数据记录_西门子触摸屏参数跟数据简单说明
  14. 日历2017 年终总结新年工作汇报PPT模板免费下载_PPTX图片设计素材_包图网888pic.com...
  15. Aliddns插件使用:小白超详细图文教程
  16. 数据仓库建设之总线矩阵/总线架构
  17. 2021年Flash被禁用后继续使用的方法
  18. 域控组策略桌面壁纸设置问题
  19. 关于AP3211KTR-G1
  20. html5学习笔记之十(微数据)

热门文章

  1. 欢乐纪中某B组赛【2019.1.26】
  2. jzoj4224-食物【多重背包】
  3. codeforces1496 D. Let‘s Go Hiking(乱搞+讨论)
  4. 纪中A组模拟赛总结(2021.7.21)
  5. 【贪心】奶牛晒衣服(ybtoj 贪心-1-1)
  6. 纪中C组模拟赛总结(2019.8.9)
  7. Summer Training day4 欧拉降幂
  8. Sentinel(五)之流量控制
  9. mybatis源码阅读(二):mybatis初始化上
  10. Spring 事务原理和使用