Jboss RestEasy构建简单的RESTful Web Services示例(1)

项目上要用到webservice,鉴于现在restful webservice比较流行,打算用restful来建立webservice,网上搜了一遍,认为Jboss的RESTEasy比较容易上手,于是就用它来小试牛刀!

RESTEasy是JBoss的一个开源项目,提供各种框架帮助你构建RESTful Web Services和RESTful Java应用程序。作为一个JBOSS的项目,它当然能和JBOSS应用服务器很好地集成在一起。但是,它也能在任何运行JDK5或以上版本的Servlet容器中运行。

Resteasy的使用很简单,主要就是配置web.xml.

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

<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>

主要的实现代码

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!

源码下载:http://www.kuaipan.cn/file/id_164037033101099036.htm

这里只是简单说了一下RestEasy创建webservice的简单使用,没有做更深入的探讨,希望大家能喷多点意见,
源码里还有war包,大家可以直接部署测试看看实际效果!

你可能感兴趣的:(jboss,webservice,Restful,resteasy)