利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)
来源:互联网 发布:抢单app源码 编辑:程序博客网 时间:2024/06/04 18:04
基于resteasy版本:2.2.1.GA
使用maven2.2.1作为构建和依赖管理工具
1.创建工程,配置pom.xml
mvn archetype:create -DgroupId=com.longtask.rest.easyrest -DartifactId=easyrest -DarchetypeArtifactId=maven-archetype-webapp
mvn eclipse:eclipse
注:使用m2eclipse插件可直接import- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>rest.resteasy</groupId>
- <artifactId>rest-resteay-demo</artifactId>
- <packaging>war</packaging>
- <version>1.0</version>
- <name>rest-resteay-demo Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <repositories>
- <repository>
- <id>java.net</id>
- <url>http://download.java.net/maven/1</url>
- <layout>legacy</layout>
- </repository>
- <repository>
- <id>maven repo</id>
- <name>maven repo</name>
- <url>http://repo1.maven.org/maven2/</url>
- </repository>
- <!-- For resteasy -->
- <repository>
- <id>jboss</id>
- <name>jboss repo</name>
- <url>http://repository.jboss.org/nexus/content/groups/public/</url>
- </repository>
- </repositories>
- <dependencies>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxrs</artifactId>
- <version>2.2.1.GA</version>
- <!-- filter out unwanted jars -->
- <exclusions>
- <exclusion>
- <groupId>commons-httpclient</groupId>
- <artifactId>commons-httpclient</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-impl</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jettison-provider</artifactId>
- <version>2.2.1.GA</version>
- <exclusions>
- <exclusion>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-impl</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.xml.stream</groupId>
- <artifactId>stax-api</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>rest-resteay-demo</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
2.编写jax-rs的服务类
- package resteasy.server;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- @Path(value = "echo")
- public class Echo {
- @GET
- @Path(value = "/{message}")
- public String echoService(@PathParam("message") String message)
- {
- return message;
- }
- }
@GET表示响应HTTP 的get方法
@PathParam表示引用URI中得参数
详细的注解可参考我下面的参考文档
3.web.xml的配置
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- <web-app>
- <context-param>
- <param-name>resteasy.resources</param-name>
- <param-value>resteasy.server.Echo</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>Resteasy</servlet-name>
- <servlet-class>
- org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Resteasy</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
3.打包部署到响应的servlet容器即可(如tomcat),然后访问http://localhost:8080/rest-resteay-demo/echo/hello,world,网页上出现hello,world则成功
hello,world可换成任意字符,同样也将返回响应的字符
注:如果不使用maven,则可以到resteasy官网下载响应jar包即可
转自于:http://blog.csdn.net/caizhh2009/article/details/6934845
0 0
- 利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)
- 利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)
- 利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)
- 利用resteasy框架构建rest webservice----第三波:实现文件上传---非form提交方式(实例、教程)
- 利用resteasy框架构建rest webservice----第三波:实现文件上传---非form提交方式(实例、教程)
- 利用resteasy框架构建rest webservice----第二波:使用不同的方式让resteasy发布我们的restful webservice 服务(实例、教程)
- 利用resteasy框架构建rest webservice----第四波:resteasy与spring真正集成发布我们的restful webservice 服务(实例、教程)
- 利用resteasy框架构建rest webservice----第二波:使用不同的方式让resteasy发布我们的restful webservice 服务(实例、教程)
- 利用resteasy框架构建rest webservice----第四波:resteasy与spring真正集成发布我们的restful webservice 服务(实例、教程)
- 利用resteasy框架构建rest webservice----第五波:java代码调用乱码解决方案
- 利用resteasy框架构建rest webservice----第五波:java代码调用乱码解决方案
- 利用resteasy框架构建rest webservice----第六波:实现文件下载
- maven构建springmvc+springsecurity+REST+Webservice框架开发CMS
- qt利用QT designer构建第一个界面helloworld工程
- WebService 实例构建
- 【WebService】快速构建WebService示例
- 快速构建Android REST客户端
- SBT(Scala Build Tool)系列教程-使用sbt构建第一个简单的HelloWorld程序
- IOS网络学习-3、搭建本地服务器
- rhel7.1的前瞻
- 万恶的12306
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- CI学习随手记(一)
- 利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)
- 自己动手写一个Struts2
- file_get_contents 小结
- Python文件夹与文件的操作
- 做营销,不要排斥所有对你说NO的人
- 数据格式化java
- ETF基金一级市场申购赎回和二级市场买入卖出
- uva 11627 Slalom
- Hash table 的避碰策略浅谈