项目上要用到webservice,鉴于如今restful webservice比较流行,打算用restful来创建webservice,网上搜了一遍,认为Jboss的RESTEasy比较容易上手,因而就用它来小试牛刀! java
RESTEasy是JBoss的一个开源项目,提供各类框架帮助你构建RESTful Web Services和RESTful Java应用程序。做为一个JBOSS的项目,它固然能和JBOSS应用服务器很好地集成在一块儿。可是,它也能在任何运行JDK5或以上版本的Servlet容器中运行。 web
Resteasy的使用很简单,主要就是配置web.xml. apache
web.xml 服务器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>resteasyExample</display-name> <!-- Auto scan rest service --> <context-param> <param-name>resteasy.resources</param-name> <param-value>com.hsbc.resteasy.helloWorld</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>其中“<param-value>com.hsbc.resteasy.helloWorld</param-value> ”为相应的类,注意大小写!,其余的配置,复制张贴就行了。
POM.xml restful
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hsbc.resteasy</groupId> <artifactId>resteasyExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>restEasy Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>JBoss repository</id> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency> </dependencies> <build> <finalName>resteasyExample</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
主要的实现代码 app
package com.hsbc.resteasy; import java.io.FileNotFoundException; import java.net.URISyntaxException; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/message") public class helloWorld { @GET @Path("getMethod/{param}") public Response getIssue(@PathParam("param") String msg) throws URISyntaxException, FileNotFoundException { String result = "Helloword "+msg; return Response.status(200).entity(result).build(); } }
看了以后,和AXIS的webservice对比起来是否是简单不少? 框架
测试示例:http://localhost:8080/resteasyExample/rest/message/getMethod/dear! maven
源码下载:http://www.kuaipan.cn/file/id_164037033101099036.htm 测试
这里只是简单说了一下RestEasy建立webservice的简单使用,没有作更深刻的探讨,但愿你们能喷多点意见,
源码里还有war包,你们能够直接部署测试看看实际效果! ui