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

JSON类型的参数与返回值,在客户端使用Ajax访问服务端时会非常有用。

加入:org.json.jar   org.restlet.ext.json.jar   如果要用Jackson工具,就再加入org.codehaus.jackson.core.jar   org.codehaus.jackson.mapper.jar  org.restlet.ext.jackson.jar


一、返回简单Json类型

服务端

	/**
	 * 返回简单JSON类型
	 */
	@Get
	public Representation getMovieInfo() throws  JSONException{
		JSONObject mailEle = new JSONObject() ;
		mailEle.put("name", "速度与激情6") ;
		mailEle.put("size", 100000l) ;
		mailEle.put("minutes", 120) ;
		return new JsonRepresentation(mailEle) ;
	}
	


客户端
启动服务器后,在浏览器输入http://localhost:8888 将会看到输出字符串:{"name":"速度与激情6","size":1000000,"minutes":120}

使用restlet库开发客户端

	@Test
	public void test02() throws IOException, JSONException{
		ClientResource client = new ClientResource("http://localhost:8888/");  
		
		//获取返回结果
		Representation result = client.get() ;
		
		JsonRepresentation jr = new JsonRepresentation(result);
		JSONObject movie = jr.getJsonObject();  
		System.out.printf("name:%s size:%d minutes:%d" ,
				movie.get("name") , 
				movie.get("size"),
				movie.get("minutes"));
	}



二、接收简单Json类型

服务端:

	/**
	 * 接收简单Json类型数据
	 */
	@Put
	public String uploadMovie(JsonRepresentation mivieJson) throws JSONException{
		JSONObject movie = mivieJson.getJsonObject();  
		String result = String.format("upload movie{name:%s size:%d minutes:%d} success!" ,
				movie.get("name") ,    
				movie.get("size"),
				movie.get("minutes"));  
		return result ;
	}
   
使用restlet库开发客户端

	@Test
	public void test03() throws IOException, JSONException{
		ClientResource client = new ClientResource("http://localhost:8888/json");  
		JSONObject mailEle = new JSONObject() ;
		mailEle.put("name", "速度与激情6") ;
		mailEle.put("size", 100000l) ;
		mailEle.put("minutes", 120) ;
		JsonRepresentation jr = new JsonRepresentation(mailEle);
		
		Representation result = client.put(jr);
		
		System.out.println(result.getText());   
	}
	



三、使用jackson工具直接将java对象转换为Representation或将Representation快速转换为Java对象

	/**
	 * 将复杂对象使用Json格式返回
	 */
	@Get
	public Representation getMovieInfo(){
		Movie movie = new Movie() ;
		movie.setName("速度与激情6");
		movie.setSize(1000000l);
		movie.setMinutes(120);     
		return new JacksonRepresentation<Movie>(movie);
	}

	/**
	 * 将接收到的Json对象直接转换成java对象
	 * @param rp
	 * @return
	 * @throws IOException
	 */
	@Put
	public String updateMovieInfo(Representation rp) throws IOException{
		JacksonRepresentation<Movie> movidRep = new JacksonRepresentation<Movie>(rp , Movie.class);
		Movie movie = movidRep.getObject() ;
		String result = String.format("upload movie{name:%s size:%d minutes:%d} success!" ,
				movie.getName() ,    
				movie.getSize(),
				movie.getMinutes());  
		return result ;
	}


因为服务端资源方法比较多,所以这次服务端使用了两个资源类分别为MovieResourceJson.java 、MovieResourceJackson.java

Server的代码也发生了变化,为每个Resource分别绑定URI

public static void main(String[] args) throws Exception {
		
		Component comp = new Component() ;
		comp.getServers().add(Protocol.HTTP, 8888) ;
		comp.getDefaultHost().attach("/json", MovieResourceJson.class) ;
		comp.getDefaultHost().attach("/jackson", MovieResourceJackson.class) ;
		comp.start(); 
		
	}
	   

然后如果是访问MovieResourceJson.java中的get方法,需要访问http://localhost:8888/json

然后如果是访问MovieResourceJackson.java中的get方法,需要访问http://localhost:8888/jackson

其他方法同上


  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值