rest服务例子

1、接口类(IHello) 
Java代码    收藏代码
  1. import javax.ws.rs.GET;  
  2. import javax.ws.rs.Path;  
  3. import javax.ws.rs.PathParam;  
  4.   
  5. import com.bean.Result;  
  6.   
  7. @Path("/find/")  
  8. public interface IHello {  
  9.   
  10.     /** 
  11.      * 登录  
  12.      * @param userName 帐号 
  13.          * @param userPass 密码 
  14.      * @return 
  15.      */  
  16.     @POST  
  17.     @Path("/login")  
  18.     Result login(@FormParam("userName")String userName,@FormParam("userPass")String userPass);  
  19.   
  20.        /** 
  21.      * 按名称查询  
  22.      * @param who 名字 
  23.      * @return 
  24.      */  
  25.     @GET  
  26.     @Path("/findByName/{who}")  
  27.     Result findByName(@PathParam("who")String who);  
  28.   
  29. }  


2、实现类(HelloImpl) 
Java代码    收藏代码
  1. public class HelloImpl implements IHello {  
  2.   
  3.     @Override  
  4.     public Result findByName(String who) {  
  5.        //逻辑实现  
  6.     }  
  7.   
  8.         @Override  
  9.     public Result login(String userName, String userPass) {  
  10.        //逻辑实现  
  11.     }  
  12.   
  13. }  


3、返回实体(Result) 
Java代码    收藏代码
  1. import java.util.List;  
  2.   
  3. import javax.xml.bind.annotation.XmlRootElement;  
  4.   
  5. @XmlRootElement(name = "result")  
  6. public class Result implements Serializable{  
  7.   
  8.     /** 
  9.      * 返回结果 
  10.      */  
  11.     private List<User> lstUser;  
  12.   
  13.     public List<User> getLstUser() {  
  14.         return lstUser;  
  15.     }  
  16.   
  17.     public void setLstUser(List<User> lstUser) {  
  18.         this.lstUser = lstUser;  
  19.     }  
  20. }  


4、app-rest.xml配置 
Xml代码    收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  3.     xsi:schemaLocation="  
  4. http://www.springframework.org/schema/beans  
  5. http://www.springframework.org/schema/beans/spring-beans.xsd  
  6. http://cxf.apache.org/jaxrs  
  7. http://cxf.apache.org/schemas/jaxrs.xsd">  
  8.   
  9.   
  10.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  13.   
  14.     <jaxrs:server id="hello" address="/hello">  
  15.         <jaxrs:extensionMappings>  
  16.             <entry key="json" value="application/json" />  
  17.             <entry key="xml" value="application/xml" />  
  18.         </jaxrs:extensionMappings>  
  19.         <jaxrs:serviceBeans>  
  20.             <ref bean="helloImpl" />  
  21.         </jaxrs:serviceBeans>  
  22.     </jaxrs:server>  
  23.   
  24.     <bean id="helloImpl" class="com.HelloImpl" />  
  25. </beans>  


5、依赖jar包 
Xml代码    收藏代码
  1. <dependency>  
  2.     <groupId>org.apache.cxf</groupId>  
  3.     <artifactId>apache-cxf</artifactId>  
  4.     <version>2.2.7</version>  
  5.     <type>pom</type>  
  6.     <scope>compile</scope>  
  7. </dependency>  
  8. <dependency>  
  9.     <groupId>javax.ws.rs</groupId>  
  10.     <artifactId>jsr311-api</artifactId>  
  11.     <version>1.0</version>  
  12.     <type>jar</type>  
  13.     <scope>compile</scope>  
  14. </dependency>  
  15. <dependency>  
  16.     <groupId>commons-httpclient</groupId>  
  17.     <artifactId>commons-httpclient</artifactId>  
  18.     <version>3.1</version>  
  19. </dependency>  
  20. <dependency>  
  21.     <groupId>org.springframework</groupId>  
  22.     <artifactId>spring</artifactId>  
  23.     <version>2.5.4</version>  
  24. </dependency>  
  25. <dependency>  
  26.     <groupId>org.springframework</groupId>  
  27.     <artifactId>spring-webmvc</artifactId>  
  28.     <version>2.5.4</version>  
  29. </dependency>  
  30. <dependency>  
  31.     <groupId>javax.servlet</groupId>  
  32.     <artifactId>servlet-api</artifactId>  
  33.     <version>2.5</version>  
  34.     <type>jar</type>  
  35.     <scope>provided</scope>  
  36. </dependency>  


6、web.xml配置 
Xml代码    收藏代码
  1. <servlet>  
  2.     <servlet-name>CXFServlet</servlet-name>  
  3.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6.   
  7. <servlet-mapping>  
  8.     <servlet-name>CXFServlet</servlet-name>  
  9.     <url-pattern>/rest/*</url-pattern>  
  10. </servlet-mapping>  


7、调用方法 
方法一: 
Java代码    收藏代码
  1. HttpClient httpClient = new HttpClient();  
  2. GetMethod getMethod = new GetMethod("http://localhost:8080/Test/rest/hello/find/findByName/jack.xml");  
  3. try {  
  4.     int status = httpClient.executeMethod(getMethod);  
  5.     if (status == 200) {  
  6.         XMLSource xmlSource = new XMLSource(getMethod.getResponseBodyAsStream());  
  7.         Result result = xmlSource.getNode("/", Result.class);  
  8.         System.out.println(result.getLstUser().size());  
  9.     }  
  10. catch (Exception e) {  
  11.     e.printStackTrace();  
  12. }  

方法二: 
Java代码    收藏代码
  1. IHello hello = (IHello) JAXRSClientFactory.create("http://localhost:8080/Test/rest/hello", IHello.class);  
  2. Result result = hello.findByName("jack");  
  3. System.out.println(result.getLstUser().size());  


8、优缺点和注意事项 
1.优缺点 
优点:跨平台(支持xml和json格式) 
缺点:效率低 
2.注意事项 
参数:如果不是java的基本类型需要封装成javabean,类必须加如下注解: 
Java代码    收藏代码
  1. @XmlRootElement  
  2. @XmlAccessorType(XmlAccessType.FIELD)  
  3. public class Param {  
  4. ...   
  5. }  

返回值:如果不是java基本类型需要封装成javabean,需要序列化,属性和类需要加如下注解: 
Java代码    收藏代码
  1. @XmlRootElement(name = "result")  
  2. public class Result implements Serializable {  
  3.  @Field  
  4.  private String id;  
  5.  ...  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用方法: 1、下载工程文件; 2、将工程文件导入到eclipse等,或者直接进行发布,如直接将工程放到【tomcat】/webapps/下即可。 3、搜索整个工程文件,将字符串8080改为你自己服务器的IP; 4、启动服务器。 测试方法: 1、main函数测试 打开Client类,运行main函数可测试post和delete方法; 2、进行单个Application测试: 1)返回所有学生信息:http://localhost:8080/RestApplication/rest/student 2)执行Client类的main函数,添加一条学生信息; 3)重复1); 4)获取ID=1的单个学生信息:http://localhost:8080/RestApplication/rest/student/1 5)打开http://localhost:8080/RestApplication/update.jsp页面,输入相关信息,提交;如果提交失败,请检查页面表单的 action属性值是否正确; 6)重复4),查看信息被修改的情况; 3、使用Component绑定多个Application 本代码源自转自【http://www.lifeba.org/arch/restlet_develop_application_component_2.html 】但有改动,主要改动有: 1. 修改了web.xml的段,使工程既可以访问rest服务,又可以访问普通的页面资源,不用再像原作者那样再单独部署一个页面工程。 2. 由于【1】的改动,使得只有以/rest开头的URL才能映射到某资源,使用rest服务时,必须要加上/rest。 3. 由于【1】的改动,RestComponent类注册application时将资源字符串加上了/rest。 4. 由于【1】的改动和本人WEB服务器端口号的不同,Client测试类的相关资源字符串也做了相应改动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值