加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

restlet2.1 学习笔记(四) 获取、返回XML类型参数

发布时间:2020-12-16 05:22:35 所属栏目:百科 来源:网络整理
导读:首先加入:org.restlet.ext.xml.jar 获取XML类型参数 服务端的定义可以用如下方式: @Postpublic String pause(DomRepresentation param) throws IOException{/* * 可以用下面这行代码将xml内容打印出来。但是如果调用下面的getText()方法。 * 就不能继续再

首先加入:org.restlet.ext.xml.jar

获取XML类型参数

服务端的定义可以用如下方式:

	
	@Post
	public String pause(DomRepresentation param) throws IOException{
		/*
		 * 可以用下面这行代码将xml内容打印出来。但是如果调用下面的getText()方法。 
		 * 就不能继续再解析param对象了。因为getText()方法会将内容从流中将内容读完
         */
		//System.out.println("xml内容:n" + param.getText() );
		
		//解析xml
		Document doc = param.getDocument() ;
		Element mailEle = doc.getDocumentElement() ;
		Element nameEle = (Element)mailEle.getElementsByTagName("name").item(0);
		Element sizeEle = (Element)mailEle.getElementsByTagName("size").item(0);
		Element minutsEle = (Element)mailEle.getElementsByTagName("minuts").item(0);
		String name  = nameEle.getTextContent() ;
		String size  = sizeEle.getTextContent() ;
		String minuts  = minutsEle.getTextContent() ;
		String result = String.format("name:%s size:%s minuts:%s",name,size,minuts);
		return result  ;
	}  
	



在这里介绍一个RestClientUI工具。可以用该工具直接调用我们的Restlet服务。

自己去google RestClient就有下载


一、使用RestClientUI工具访问客户端

第1步,URL中输入http://localhost:8888/

第2步,在Method选项卡下的单选按钮中选择Post

第3步,在Body选项卡下的下拉框选择String Body,然后在文本框中输入如下内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><movie><name>速度与激情6</name><size>10000000</size><minuts>120</minuts></movie>

第四步,提交请求。会看到返回内容:name:速度与激情6 size:10000000 minuts:120



二、使用Restlet的客户端支持开发客户端

	@Test
	public void test02() throws IOException{
		ClientResource client = new ClientResource("http://localhost:8080/");  
		
		//创建请求参数
		DomRepresentation param = new DomRepresentation(MediaType.APPLICATION_XML);
		Document doc = param.getDocument() ;
		//创建节点
		Node movieNode = doc.createElement("movie");	
		Node nameNode = doc.createElement("name");
		Node sizeNode = doc.createElement("size");
		Node minutsNode = doc.createElement("minuts");
		//设置值
		nameNode.setTextContent("速度与激情6");
		sizeNode.setTextContent("10000000");
		minutsNode.setTextContent("120");
		
		movieNode.appendChild(nameNode);
		movieNode.appendChild(sizeNode);
		movieNode.appendChild(minutsNode);
		
		doc.appendChild(movieNode);  
		
		
		Representation result =  client.post(param) ;		//调用post方法
		System.out.println(result.getText());  
	}
	

同样会打印出结果:name:速度与激情6 size:10000000 minuts:120



同时DomRepresentation对象也支持使用Xpath来解析。在服务端多加上一个方法。加上@Put注解。

	
	@Put
	public String put(DomRepresentation param) throws IOException{
		/**
		 * 使用XPath方式来解析XML
		 */
		String name  = param.getText("/movie/name");
		String size  = param.getText("/movie/size");
		String minuts  = param.getText("/movie/minuts");
		String result = String.format("name:%s size:%s minuts:%s",minuts);
		return result  ;
	}
	

然后在使用RestClientUI测试时只需要将Method选项卡中的单选按钮Post改为Put。然后再测试就能访问服务器端的Put方法了



返回XML类型参数

在服务端再加入一个getMovie方法,使用@Get注解

	@Get
	public DomRepresentation getMovieInfo() throws IOException{
		//创建请求参数
		DomRepresentation movieInfo = new DomRepresentation(MediaType.APPLICATION_XML);
		Document doc = movieInfo.getDocument() ;
		//创建节点
		Node movieNode = doc.createElement("movie");	
		Node nameNode = doc.createElement("name");
		Node sizeNode = doc.createElement("size");
		Node minutsNode = doc.createElement("minuts");
		//设置值
		nameNode.setTextContent("速度与激情6");  
		sizeNode.setTextContent("10000000");
		minutsNode.setTextContent("120");

		movieNode.appendChild(nameNode);
		movieNode.appendChild(sizeNode);
		movieNode.appendChild(minutsNode);

		doc.appendChild(movieNode);  

		return movieInfo ;
	}

然后就可以用浏览器访问http://localhost:8080/就可以看到返回的xml信息了。

也可以使用RestClient客户端编写代码调用。客户端如下:

	
@Test
public void test04() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/"); 
Representation result = client.get() ; 
DomRepresentation dr = new DomRepresentation(result);
String name = dr.getText("/movie/name");
String size = dr.getText("/movie/size");
String minuts = dr.getText("/movie/minuts");
System.out.printf("name:%s size:%s minuts:%s",minuts);
}
	
输出结果为<?xml version="1.0" encoding="UTF-8" standalone="no"?><movie><name>速度与激情6</name><size>10000000</size><minuts>120</minuts></movie>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读