Jersey hello world example
2015-08-27 10:37
573 查看
Jersey, reference implementation to develope RESTful web service based on the JAX-RS (JSR 311) specification.
In this tutorial, we show you how to develop a simple hello world REST web application with Jersey.
Technologies and Tools used in this article:
Jersey 1.8
JDK 1.6
Tomcat 6.0
Maven 3.0.3
Eclipse 3.6
File :
File :
And the “
URL : http://localhost:8080/RESTfulExample/rest/hello/mkyong
In this tutorial, we show you how to develop a simple hello world REST web application with Jersey.
Technologies and Tools used in this article:
Jersey 1.8
JDK 1.6
Tomcat 6.0
Maven 3.0.3
Eclipse 3.6
1. Directory Structure
This is the final web project structure of this tutorial.2. Standard Web Project
Create a standard Maven web project structure.mvn archetype:generate -DgroupId=com.mkyong.rest -DartifactId=RESTfulExample -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Note
To support Eclipse, use Maven command :mvn eclipse:eclipse -Dwtpversion=2.0
3. Project Dependencies
Jersey is published inJava.netMaven repository. To develop Jersey REST application , just declares “
jersey-server” in Maven
pom.xml.
File : pom.xml
<project ...> <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> </dependencies> </project>
4. REST Service
Simple REST service with Jersey.package com.mkyong.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/hello") public class HelloWorldService { @GET @Path("/{param}") public Response getMsg(@PathParam("param") String msg) { String output = "Jersey say : " + msg; return Response.status(200).entity(output).build(); } }
5. web.xml
Inweb.xml, register “
com.sun.jersey.spi.container.servlet.ServletContainer“, and puts your Jersey service folder under “
init-param“, “
com.sun.jersey.config.property.packages“.
File : web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Restful Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.mkyong.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
6. Demo
In this example, web request from “projectURL/rest/hello/” will match to “
HelloWorldService“, via
@Path("/hello").
And the “
{any values}” from “
projectURL/rest/hello/{any values}” will match to parameter annotated with
@PathParam.
URL : http://localhost:8080/RESTfulExample/rest/hello/mkyong
相关文章推荐
- LDAP使用手册
- python中 import gevent报错:libevent version mismatch
- Gradle使用手册(三):构建任务
- 初步了解OPENWRT
- 错误:ORA-12537: TNS: 连接关闭
- Red Hat Linux6:postfix邮件系统
- Quartz集成Spring框架
- Libvirt错误总结
- 存储在session中的类要进行系列化
- 关于spark的一些简单认识。
- 版本控制——hg教程
- linux openldap同步配置
- concurrentHashMap原理分析
- SSH公钥登录访问(配置实验)
- lpad函数介绍
- Unity鼠标自由查看3D物体之直接旋转物体
- #2334:Simple prefix compression--关于string.length()
- oracle数据库解决查询值为null的情况
- freemarker全解教程
- Yii2 Component基类分析