`

restlet jax-rs配置

    博客分类:
  • rest
 
阅读更多

 

配置基于 RESTlet JAX-RS Extension 的 Web Service 也就是部署该架构下的 Web Service。RESTlet 架构提供两种部署 Web Service 的方式。两种方式都方便简单,用户可以根据自己的需求选择任意一种部署方式。

  • 将 Web Service 当做单独的 Java 程序进行部署
  • 将 Web Service 部署到 Servelet Container 中

两种方式都方便简单,用户可以根据自己的需求选择任意一种部署方式。

将 Web Service 部署成一个单独运行的 Java 应用非常的简单,只需要完成以下几个步骤。

  • 导入需要的 JAR 包,org.restlet.jar,以及org.restlet.ext.jaxrs_1.0.jar
  • 为 HTTP Server 创建相应 Java 类。在新建的 Java 类中依次完成以下工作,引入 org.restlet.jar 包中需要的类,新建 HTTP Server,定义该 Server 监听的端口,将 Web Service 的配置类加入到 HTTP 服务器中。
  • 编译运行 HTTP Server。

将基于 RESTlet Jax-Rs Extension 的 Web Service 部署到 Servelet Container 中的过程和部署一个基本的 Servelet 极其相似。不同的是,部署过程中,用户需要注意添加需要的 Jar 包。以下 Jar 是该部署方式所需要的。

为了成功将基于 RESTlet Jax-Rs Extension 的 Web Service 部署为 Servelet,用户需要完成以下动作。

  • 编译基于 RESTlet Jax-Rs Extension 的 Web Service 包含的代码。
  • 将需要的 JAR 包存放于 /WEB-INF/lib 中。
  • 创建 Servelet 的配置文件 web.xml。
  • 将所有相关内容打包成 WAR 包,并部署到用户选定的 Servelet 容器中。

 

 

package com.resource;

 

import java.util.ArrayList;

import java.util.List;

 

import javax.ws.rs.*;

 

import com.model.User;

import com.model.UserGroup;

import com.model.UserGroupManager;

import com.model.UserManager;

 

@Path("users")

public class JaxRsExtensionResource {

 

@GET

@Path("usergroup")

public String getUserGroup() {

return "Group are used to classify different kind of users!";

}

 

 

@GET

@Path("user")

public String getUser(){

return "Users inlcudes the information of registered user!";

}

 

@GET

@Path("user/{id}")

public String findUser(@PathParam("id") String id){

User temp = UserManager.get().getDetails(id);

if(temp != null)

return temp.toString();

else

return "The user you queried(ID:" + id + ") doesn't existed!";

}

 

@GET

@Path("usergroup/{id}")

public String findUserGroup(@PathParam("id") String id){

UserGroup group = UserGroupManager.get().getDetails(id);

if(group != null)

return group.toString();

else

return "The group you queried(ID:" + id + ") doesn't existed!";

}

 

}

 

@path指定资源的路由,可以有资源的路由和资源方法的路由。

@GET指定请求的类型(get,put,post,delete)

@PathParam与@path中的参数对应

 

 

首先由jax-rs管理resource

package com.application;

 

import java.util.HashSet;

import java.util.Set;

 

import javax.ws.rs.core.Application;

 

import com.resource.JaxRsExtensionResource;

 

public class ExampleApplication extends Application {

 

@Override

public Set<Class<?>> getClasses() {

Set<Class<?>> rrcs = new HashSet<Class<?>>();

rrcs.add(JaxRsExtensionResource.class);

return rrcs;

}

 

}

注意这里继承的是jax-rs的Application,不是restlet的。

再交给restlet管理

RESTlet 架构为了更好的支持 JAX-RS 规范,定了 JaxRsApplication 类来初始化基于 JAX-RS 的 Web Service 运行环境。JaxRSApplication 类使用起来非常的方便,只需要将原本基于 RESTlet 架构的应用类加入到用户自己实现的 JaxRsApplication 子类中即可。如果需要认证功能的话,使用 JaxRsApplication 的 setGuard(...) 或者 setAuthentication(...) 方法即可。本例中不设置到认证功能,所以只需要将 ExampleApplication 类加入到本例实现 JaxRsApplication 子类中即可

 

 

package com.application;

 

import org.restlet.Context;

import org.restlet.ext.jaxrs.JaxRsApplication;

 

public class JaxRsExtensionApplication extends JaxRsApplication {

public JaxRsExtensionApplication(Context context) {

super(context);

this.add(new ExampleApplication());

}

 

public static void main(){

System.out.println("Hello");

}

}

 

 

将 Web Service 当做单独的 Java 程序进行部署

 

public class JaxRsExtensionServer { 
     public static void main(String[] args){ 
         try{ 
             Component component = new Component(); 
             component.getServers().add(Protocol.HTTP, 8182);         
             component.getDefaultHost().attach(new JaxRsExtensionApplication(null)); 
             component.start(); 
         }catch(Exception e){ 
             e.printStackTrace(); 
         } 
     } 
 }
运行代码,打开浏览器输入http://localhost:portnum/users/usergroup 调用getUserGroup方法
将 Web Service 部署到 Servelet Container 中
将基于 RESTlet Jax-Rs Extension 的 Web Service 部署到 Servelet Container 中的过程和部署一个基本的 Servelet 极其相似。
不同的是,部署过程中,用户需要注意添加需要的 Jar 包。然后,创建 Servelet 的配置文件 web.xml。下面是为本例所写的配置文件。
最后用户需要将这些一起打包成 WAR 包,并部署到用户选定的 Servelet 容器中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<context-param>  
      <param-name>org.restlet.application</param-name>  
      <param-value>  
         com.application.JaxRsExtensionApplication   
      </param-value>  
   </context-param>  
  
   <!-- Restlet adapter -->  
   <servlet>  
      <servlet-name>RestletServlet</servlet-name>  
      <servlet-class>  
         com.noelios.restlet.ext.servlet.ServerServlet   
      </servlet-class>  
   </servlet>  
  
   <!-- Catch all requests -->  
   <servlet-mapping>  
      <servlet-name>RestletServlet</servlet-name>  
      <url-pattern>/*</url-pattern>  
   </servlet-mapping>  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
将工程发布指容器,运行,输入http://localhost:portnum/工程名/users/usergroup,我的chrome显示不出来不知什么原因。
模型类代码
package com.model;
public class User {
private String username = "User Sample";
private String id = "";
public User(String id){
this.id = id;
}
public User(){
this.username = "Name Sample";
this.id = "ID ISample";
}
public User(String name, String id){
this.username = name;
this.id = id;
}
public String getUser(){
if(id.equalsIgnoreCase("null"))
return "The user you queried(ID:" + this.username + ") doesn't existed!";
return "User - Name:" + this.username + " ID:" + this.id + "/n";
}
public void setName(String username){
this.username = username;
}
public String getName(){
return this.username;
}
public void setID(String id){
this.id = id;
}
public String getID(){
return id;
}
public String toString(){
if(id.equalsIgnoreCase("null"))
return "The user you queried(ID:" + this.username + ") doesn't existed!";
return "User - Name:" + this.username + " ID:" + this.id + "/n";
}
}
package com.model;
import java.util.ArrayList;
import java.util.List;
public class UserGroup {
private String groupname = "User Group Sample";
private String groupid = "User Group ID Sample";
private List<String> userlist = new ArrayList<String>();
public UserGroup(String name){
groupname = name;
}
public UserGroup(){
}
public UserGroup(String name, String id){
this.groupname = name;
this.groupid = id;
}
public String getName(){
return groupname;
}
public void setName(String name){
this.groupname = name;
}
public String getID(){
return this.groupid;
}
public void setID(String id){
this.groupid = id;
}
public void addUser(String id){
this.userlist.add(id);
}
public void deleteUser(String id){
int location = -1;
for(int i = 0; (i < this.userlist.size()) && location == -1; i++){
String temp = this.userlist.get(i);
if(temp.equalsIgnoreCase(id))
location = i;
}
this.userlist.remove(location);
}
public String getGroup(){
if(this.groupid.equalsIgnoreCase("null"))
return "Group(ID:" + this.groupname + ") does not existed!/n";
return "Group - ID:" + this.groupid + " Name:" + this.groupname + "/n";
}
public String toString(){
if(this.groupid.equalsIgnoreCase("null"))
return "Group(ID:" + this.groupname + ") does not existed!/n";
return "Group - ID:" + this.groupid + " Name:" + this.groupname + "/n";
}
}
package com.model;
import java.util.ArrayList;
import java.util.List;
public class UserGroupManager {
private static UserGroupManager manager = new UserGroupManager();
private List<UserGroup> groupList = new ArrayList<UserGroup>();
public static UserGroupManager get(){
return manager;
}
private UserGroupManager(){
final UserGroup group1 = new UserGroup();
group1.setID("1");
group1.setName("Sample Group");
groupList.add(group1);
}
public UserGroup getDetails(String id){
for(int i = 0; i < groupList.size(); i++){
UserGroup temp = this.groupList.get(i);
if(temp.getID().equalsIgnoreCase(id))
return temp;
}
return new UserGroup(id,"null");
}
}
package com.model;
import java.util.ArrayList;
import java.util.List;
public class UserManager {
private static UserManager manager = new UserManager();
public static UserManager get() {
return manager;
}
private List<User> userList = new ArrayList<User>();
private UserManager()
{
final User user1 = new User();
user1.setID(Integer.toString(1));
user1.setName("Zhou Peng");
this.userList.add(user1);
final User user2 = new User();
user2.setID(Integer.toString(2));
user2.setName("Lu Hong Yong");
this.userList.add(user2);
}
public User getDetails(String id){
for(int i = 0; i < this.userList.size(); i++){
User temp = this.userList.get(i);
if(temp.getID().equalsIgnoreCase(id))
return temp;
}
return(new User(id,"null"));
}
}

 

原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-jsr311/index.html

 

分享到:
评论

相关推荐

    restlet_jax-rs列子(客户端+服务端)

    restlet_jax-rs列子 可直接运行 在我的博客里有一些描述。希望能给大家提供点帮助

    JAX-RS Rest RestLet项目源码

    JAX-RS Rest RestLet项目源码 对初学者有帮助 实现最简单的 GET PUT DELETE UPDATE

    JAX-RS之rest接口实例

    com.rest.restlet.RestJaxRsServer 服务启动类 com.rest.restlet.client.Client 客户端调用类 eclipse项目直接使用。

    restlet-jaxrs

    用法从项目根目录运行: mvn package -Pdist Maven 将在目标子目录中创建一个名为restlet-jaxrs-dist.zip的 zip 分发restlet-jaxrs-dist.zip 。 嵌入式 Web 服务器可以使用 zip 目录bin中的脚本运行: Windows : ...

    integration-camel-restlet:在CXF JAX-RS上将集成服务作为Jar集成到外部Rest服务

    一体化骆驼座如何通过HTTP与两个服务进行互通的示例: 其中之一是集成服务,该服务使用Camel和... 第二个是外部独立的Rest服务,该服务基于CXF JAX-RS,打包为WAR存档+交付以在带有端口9094的Jetty服务器上启动。

    NCC-OpenAPI示例文档.zip

    UAP在Restlet框架之上,选择了官方JAX-RS扩展,并且在扩展的基础上与NC进行了集成。 主要jar包 modules/uapws/pubuapfw_restframeworkLevel-1.jar UAP平台的扩展主要集中在uap.ws.*中,同时还少量修改了官方的jax-rs...

    RESTful Java Web Services (2009).pdf

    technology stack together with any of the frameworks Jersey's JAX-RS, Restlet's Lightweight REST, JBoss's JAX-RS RESTEasy, and Struts 2 with the REST plugin. You don't need to know REST, as we cover ...

    RESTLET开发

    RESTLET开发实例(一)基于JAX-RS的REST服务

    Restlet学习的三篇文章

    共三篇文章,第一篇讲述了基于JAX-RS的rest服务开发,第二篇讲基于Restlet的rest服务开发,第三篇讲restlet与spring整合

    JAVA.WEB服务.构建与运行

     《Java Web 服务:构建与运行(影印版)》提供了对Java的API的一个全面介绍,包括针对XML Web服务的JAX-WS和针对RESTful Web服务的JAX-RS。《Java Web服务:构建与运行》通过提供混合架构概述、完整的工作代码示例...

    org.restlet.ext.spring.jar

    org.restlet.ext.spring.jar

    spring加载restful(文档+程序源码)

    虽然,对REST的支持并不是JAX-RS的一种实现,但是它具有比标准定义更多的特性。REST支持被无缝整合到Spring的MVC层,它可以很容易应用到使用Spring构建的应用中。  Spring REST支持的主要特性包括:  注释,如@...

    restful restful所需要的jar包

    * Implementation of the JAX-RS standard API (based on draft JSR-311). * Deployment as native services is possible and illustrated using the powerful Java Service Wrapper. * Extensive integration ...

    hikaku:一个测试REST-API实现是否符合其规范的库

    hikaku:一个测试REST-API实现是否符合其规范的库

Global site tag (gtag.js) - Google Analytics