項目上要用到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