Building and testing the service
Now that you know how you'll work with XML and already have a data layer to use, it's time to continue building your RESTful application with Restlets and do some test preparation.
现在你知道了如何使用XML和已经有了可以直接使用的数据层级,现在要开始构建RESTful应用了,先做点实践的准备。
The Races service
Recall that Acme Racing would like its service to enable clients to view existing races as well as create new ones.You've already outlined the RESTful URI that'll support this behavior:/race.
Via the Router class in the RaceApplication class, you linked this URI to the RacesResource class. You already know that you must implement three methods:
通过RaceApplication类,你将URI与RacesResource类链接起来,你需要实现下面三个方法。
*getRepresentation()
*allowPost()
*post()
Accordingly,create a class called RacesResource and ensure that it extends org.restlet.resource.Resource.Also,implement a three-parameter constructor,as shown in Listing13:
因此,创建RacesResource类并确定他继承org.restlet.resource.Resource。实现他拥有三个参数的构造方法如下:
public class RacesResource extends Resource {
public RacesResource(Context context, Request request, Response response) {
super(context, request, response);
}
}
Restlets must be instructed how to communicate resource representations properly.Because XML will serve as the resources format,you must direct your restlet by adding an XML variant type.Variants,in Restlets,represent a format for Resources.The base class,Resource,contains a getVariants() method that facilitates adding various Variant types.Accordingly,add the line in Listing14 to our constructor:
Restlets必须被指示怎么联系适当的resource。因为XML被当做resource的格式。你必须通过XML的variant类型来指示你的restlet。Variants,在Restlets中,代表Resource的格式。Resource基础类中,有一个getVariants()方法来方便添加各种Variant。如下:
this.getVariants().add(new Variant(MediaType.TEXT_XML));
The Restlet framework supports a wide variety of media types,including images and video.
Handling GET requests
Now it's time to implement the easiest behavior of the class: handling a GET request. Override the getRepresentation() method as shown in Listing 15:
下面是最简单的get请求的实现
public Representation getRepresentation(Variant variant) {
return null;
}
As you can see, this method returns a Representation type, of which there are multiple implementations. One implementation — aptly dubbed StringRepresentation — represents strings and will suffice for your needs.
As you know, you already have a legacy domain model that supports working with the database. It also turns out that someone has already written a utility class, called RaceReporter, that transforms domain objects into XML documents. This class's racesToXml() method takes a collection of Race instances and returns a String representing an XML document looking something like Listing 16:
如你所见,这个方法返回Representation 类型,这个类型有多种实现。一种实现-贴切的被称为StringRepresentation-代表会根据你的需要返回字符串。
如你所知的,你已经有了一个遗留的域对象支持数据库。他表明了有人已经写了一个有效的类,名叫RaceReporter,来讲域对象转化为XML文件。这个类的racesToXml方法返回的XML如下:
<acme-races>
<races>
<race name="Leesburg 5K" date="2008-05-12" distance="3.1" id="5">
<uri>/races/5</uri>
<description/>
</race>
<race name="Leesburg 10K" date="2008-07-30" distance="6.2" id="6">
<uri>/races/6</uri>
<description/>
</race>
</races>
</acme-races>
In fact,this XML document is an example of what your RESTful Web service will return when the /race URI is invoked with a GET request.
事实上,这个XML是你的RESTful Web服务器将对/race URI GET请求返回的一个例子。
Therefore,your job is to link the retrieval of all race instances in the underlying data store; in fact ,at this point,you can already write a test.
Testing the service
Using the Restlet framework,you can construct a client instance and have it invoke your RESTful Web service.Moreover,you can leverage XMLUnit to verify that the service's output is some known XML document. Last, but not least, you can also use DbUnit (see Resources) to put the underlying database into a known state (so you can always get back the same XML
Using JUnit 4, you can create two fixtures that properly initialize XMLUnit and DbUnit, as shown in Listing 17:
@Before
public void setUpXMLUnit() {
XMLUnit.setControlParser(
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setTestParser(
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setSAXParserFactory(
"org.apache.xerces.jaxp.SAXParserFactoryImpl");
XMLUnit.setIgnoreWhitespace(true);
}
@Before
public void setUpDbUnit() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
IDatabaseConnection conn =
new DatabaseConnection(
getConnection("jdbc:hsqldb:hsql://127.0.0.1", "sa", ""));
IDataSet data = new FlatXmlDataSet(new File("etc/database/race-db.xml"));
try {
DatabaseOperation.CLEAN_INSERT.execute(conn, data);
} finally {
conn.close();
}
}
In the setUpDbUnit method, an XML representation of the database is inserted into the database via the CLEAN_INSERT command. This XML file effectively inserts six different races. Therefore, the response to a GET will be an XML document with six races.
Next, you can create a test case that invokes an HTTP GET on the /race URI, obtains the response XML, and compares it to a control XML file using XMLUnit's Diff class, as demonstrated in Listing 18:
@Test
public void getRaces() throws Exception {
Client client = new Client(Protocol.HTTP);
Response response =
client.get("http://localhost:8080/racerrest/race/");
Diff diff = new Diff(new FileReader(
new File("./etc/control-xml/control-web-races.xml")),
new StringReader(response.getEntity().getText()));
assertTrue(diff.toString(), diff.identical());
}
分享到:
相关推荐
Metamorphic Testing of RESTful Web APIs Metamorphic Testing 是一种测试技术,旨在通过改变输入数据或环境来观察软件系统的行为变化,从而判断其正确性。RESTful Web APIs 是一种基于 Representational State of...
Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API...
Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API...
Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask...
在IT行业中,RESTful(Representational State Transfer)是一种软件架构风格,用于设计网络应用程序,尤其在Web服务领域广泛应用。C#作为.NET框架的主要编程语言,提供了丰富的工具和技术来实现RESTful服务。本篇...
Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API...
在RESTful.NET中,`restService`是核心概念之一,它代表了一个服务端的资源处理单元。通过定义RESTful接口,开发者可以构建出易于理解、可维护的API。RESTful服务通常由一组URI(统一资源标识符)组成,每个URI对应...
Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web...
本示例是关于如何使用C#语言创建一个简单的RESTful服务端以及对应的RESTful客户端。以下是相关知识点的详细说明: 1. **RESTful原则**:REST(Representational State Transfer)的核心思想是资源(Resource)和...
总结来说,谷歌浏览器RESTful请求插件是开发人员必备的工具之一,尤其对于需要频繁与Elasticsearch交互的项目。通过这个插件,用户可以方便地发起REST请求,测试和调试API,同时学习和理解RESTful架构风格。无论是在...
RESTful开发是现代Web服务开发的核心技术之一,尤其在API设计中广泛应用。 1. RESTful原则: - 客户端-服务器架构:客户端与服务器之间无状态,每个请求包含所有必要的信息。 - 统一接口:包括资源识别(通过URI...
### RESTful接口文档模板知识点解析 #### 一、RESTful接口概述 REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,可以使用XML或者JSON格式传输数据,一般用于...
RESTful接口是一种遵循REST(Representational State Transfer,表述性状态转移)架构约束的Web服务设计风格,用于构建可扩展、高性能的互联网应用程序。REST强调通过HTTP协议中的动词(GET、POST、PUT、DELETE等)...
"Java调用Restful API接口的方式" Java调用Restful API接口是Java开发中非常重要的一部分,了解Java调用Restful API接口的方式可以帮助开发者更好地理解和使用相关技术。本文将详细介绍Java调用Restful API接口的...
C#服务端RestFul Service开发经验案例 本文档介绍了如何使用C#语言创建服务端RestFul Service接口,并提供了详细的代码说明,方便用户学习和深入掌握。该经验案例主要讲解了如何使用RestFul数据访问方式将装备软件...
### C# 服务端调用 RestFul Service 的方法 #### 概述 本文档将详细介绍如何使用 C# 创建和调用 RESTful 接口,包括 RESTful 的基本概念、如何构建 RESTful 风格的 API、服务端的具体实现步骤以及客户端如何调用...
在开发RESTful接口时,我们需要遵循一定的设计规范来确保接口的一致性、可维护性和易用性。RESTful API(Representational State Transfer,也称为RESTful web服务)是一种提供互联网计算机系统间互操作性的方法。...
本文将深入探讨如何在Spring MVC中实现RESTful风格的CRUD操作,这对初学者尤其有价值。 首先,了解REST(Representational State Transfer,表述性状态转移)是一种网络应用程序的设计风格和开发方式,基于HTTP协议...
#### 六、总结 RESTful Web Services 为开发者提供了一种构建高效、可扩展且易于维护的 Web 应用程序的方法。通过使用 PHP 作为开发语言,开发者可以轻松地实现 RESTful Web Services,并利用 PHP 强大的功能来处理...
在Java开发中,HTTPClient和RESTful风格的接口被广泛用于实现文件的上传与下载功能。HTTPClient是一个强大的HTTP客户端库,而RESTful是一种轻量级的、基于HTTP协议的软件架构风格,常用于构建Web服务。在分布式系统...