Prologue:
Most of the time, when we need a webservice we setup a soap service. With tools like Spring and jax-ws this became quite easy. Sometimes You need a service but soap is too much and too complicated. So we rely on rest.
The biggest difference between SOAP and restfull is: soap is a xml structured protocol on top of the http protocol. Rest is still hitching along on the http protocol. Beside the states get and post you can also use put and delete.
Setting up:
The setup of a rest service is allmost as easy as setting up a soap service. You can use maven3 for the dependency management. The pom would look something like this:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1-ea</version>
</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-servlet</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</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-client</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>simplerestservice</finalName>
</build>
Ofcourse you can download the libraries manually and put them in the WEB-INF/lib folder.
The code:
@Path("test")
public class AutoComplete {
@GET
@Produces("text/plain")
public String getAutoComplete(){
return "this will get some data";
}
@DELETE
@Produces("text/plain")
public String getAutoComplete2(){
return "this is the functioanality too delete.";
}
All the annotations you see here are the jax-ws-rs annotations (The dependency is in the pom).
The @path annotation is responsible for initializing the endpoint of this service. In this case the url would look something like : http:mypersonaldomain.com/test. This will be for the get and the delete. In javascript or html you will set the http method to get or delete in this case.
For example : <form method = "GET"/> or <form method = "DELETE"/>
Conclusion:
Sometimes it is wise to reconcider if a soap service is what you need. If it is a simple webservice that handles only data in the end, you might concider using a restfull service.
Have fun!
Geen opmerkingen:
Een reactie posten