I have written a number of posts on JAX-RS RESTEasy concepts and how to. Now I have started exploring Jersey which is another popular framework for making RESTFul applications. To start with, I am writing my hello world application in this post, which I will modify in next posts to show demos of other features Jersey provide.
Step 1) Make a eclipse web project using maven
Step 2) Update Jersey dependencies in pom.xml file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" < modelVersion >4.0.0</ modelVersion > < groupId >com.howtodoinjava</ groupId > < artifactId >JerseyHelloWorld</ artifactId > < packaging >war</ packaging > < version >1.0-SNAPSHOT</ version > < name >JerseyHelloWorld Maven Webapp</ name > < repositories > < repository > < id >maven2-repository.java.net</ id > < name >Java.net Repository for Maven</ name > < layout >default</ layout > </ repository > </ repositories > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >com.sun.jersey</ groupId > < artifactId >jersey-server</ artifactId > < version >1.17.1</ version > </ dependency > < dependency > < groupId >com.sun.jersey</ groupId > < artifactId >jersey-core</ artifactId > < version >1.17.1</ version > </ dependency > < dependency > < groupId >com.sun.jersey</ groupId > < artifactId >jersey-servlet</ artifactId > < version >1.17.1</ version > </ dependency > </ dependencies > < build > < finalName >JerseyHelloWorld</ finalName > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < configuration > < source >1.6</ source > < target >1.6</ target > </ configuration > </ plugin > </ plugins > </ build > </ project > |
Step 3) Update web.xml file with servlet mapping to com.sun.jersey.spi.container.servlet.ServletContainer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" < web-app > < display-name >Archetype Created 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.howtodoinjava.rest</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >jersey-serlvet</ servlet-name > < url-pattern >/rest-points/*</ url-pattern > </ servlet-mapping > </ web-app > |
Step 4) Write first REST service class
The service class will look like exactly as RESTEasy because of common annotations used from JAX-RS.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| package com.howtodoinjava.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path ( "/show-on-screen" ) public class JerseyHelloWorldService { @GET @Path ( "/{message}" ) public Response getMsg( @PathParam ( "message" ) String msg) { String output = "Message requested : " + msg; //Simply return the parameter passed as message return Response.status( 200 ).entity(output).build(); } } |
No comments:
Post a Comment