On this tutorial I’ll display you what Relaxation is and how you can develop a easy relaxation net service in java utilizing jersey. Earlier than transferring ahead let see what relaxation is.
- REST stands for Representational State Switch. (It’s generally spelled “ReST”.)
- It depends on a stateless, client-server, cacheable communications protocol which makes use of HTTP protocol.
- REST is a less complicated, light-weight various to RPC (Distant Process Calls) and Net Companies (SOAP, WSDL, and so on). Know extra different between Rest and SOAP.
I’ll use solar’s JAX-RS reference implementation Jersey to display easy greeting service.
Create new Java EE undertaking in eclipse and add all dependent jar information in classpath. You’ll be able to obtain associated jars from https://jersey.java.net website.
Your net.xml will look just like …
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
xml model=“1.0” encoding=“UTF-8”?>
<net–app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns=“http://java.solar.com/xml/ns/javaee” xmlns:net=“http://java.solar.com/xml/ns/javaee/web-app_2_5.xsd”
xsi:schemaLocation=“http://java.solar.com/xml/ns/javaee http://java.solar.com/xml/ns/javaee/web-app_3_0.xsd”
id=“WebApp_ID” model=“3.0”>
<show–identify>RESTfulTest</show–identify>
<servlet>
<servlet–identify>jersey–serlvet</servlet–identify>
<servlet–class>com.solar.jersey.spi.container.servlet.ServletContainer</servlet–class>
<init–param>
<param–identify>com.solar.jersey.config.property.packages</param–identify>
<param–worth>org.techzoo.restful</param–worth>
</init–param>
<load–on–startup>1</load–on–startup>
</servlet>
<servlet–mapping>
<servlet–identify>jersey–serlvet</servlet–identify>
<url–sample>/relaxation/*</url–sample>
</servlet–mapping>
</net–app>
|
Right here you possibly can see net.xml
comprise one servlet named ServletContainer
which has url sample related to it.
Now write one class which is our foremost REST net service class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
bundle org.techzoo.restful;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path(“/greeting”)
public class GreetingService {
@GET
@Path(“/{identify}”)
public Response sayGreeting(@PathParam(“identify”) String msg) {
String output = String.format(“Greeting to %s from Jersey”, msg);
return Response.standing(200).entity(output).construct();
}
}
|
This REST based mostly net service can be accessable utilizing following URL.
http://localhost:8080/RESTfulTest/relaxation/greeting/tousif
Supply techzoo.org