Restful WebService is very easy to understand instance

RESTful WebService is a kind of lightweight Web service that is much simpler than SOAP message-based WebService. RESTful WebService is stateless, and it is very easy to publish and call.
Here is a simple example of Hello World, in order to have a perceptual understanding of RESTful WebService. Because very professional and theoretical description of RESTful WebService is a painful thing to understand. Look at the examples to get a general idea, and then look at the theory and it will be easier to understand.
/**
* Getting started with RESTful WebService< br> * @author leizhimin 2009-11-18 16:42:43
*/

package example;
< br> import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;

//指定URI
@Path( “/helloworld”)
public class HelloWorld {
//Handle HTTP GET requests
@GET
// The content format of the processing request feedback is “text/plain”
@Produces( “text/plain”)
public String getClichedMessage() {
return “Hello World!”;
        }

         public static void main(String[] args) throws IOException {
//Create a RESTful WebService service
HttpServer server = HttpServerFactory.create( “http://192.168.14.117:9999/”);
//Start the service, which will cause a new thread to be opened
server.start();
//Output some prompt information of the service to the control Taiwan
System.out.println(“RESTful WebService service has started”);
System.out.println(“Service access address: http://192.168.14.117:9999/helloworld”);
}
}

Run this class, the console input:
2009-11-18 17:25:37 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Scanning for root resource and provider classes in the paths:
D:\jdk16\jre\lib\alt-rt.jar
D:\jdk16\jre\lib\charsets.jar < br> D:\jdk16\jre\lib\deploy.jar
D:\jdk16\jre\lib\javaws.jar
D:\jdk16\jre\lib\jce.jar
D:\ jdk16\jre\lib\jsse.jar
D:\jdk16\jre\lib\management-agent.jar
D:\jdk16\jre\lib\plugin.jar
D:\jdk16\jre \lib\resources.jar
D:\jdk16\jre\lib\rt.jar
D:\jdk16\jre\lib\ext\dnsns.jar
D:\jdk16\jre\lib\ ext\localedata.jar
D:\jdk16\jre\lib\ext\sunjce_provider.jar
D:\jdk16\jre\lib\ext\sunmscapi.jar
D:\jdk16\jre\lib \ext\su npkcs11.jar
D:\netwideo\restws\out\production\restws
D:\IDEA8\lib\javaee.jar
D:\netwideo\restws\lib\mail-1.4.jar
D:\netwideo\restws\lib\asm-3.1.jar
D:\netwideo\restws\lib\wadl2java.jar
D:\netwideo\restws\lib\jettison-1.0.1.jar
br> D:\netwideo\restws\lib\grizzly-servlet-webserver-1.8.6.4.jar
D:\netwideo\restws\lib\wadl-core.jar
D:\netwideo\restws\lib \localizer.jar
D:\netwideo\restws\lib\jdom-1.0.jar
D:\netwideo\restws\lib\jsr311-api-1.0.jar
D:\netwideo\restws\ lib\stax-api-1.0-2.jar
D:\netwideo\restws\lib\persistence-api-1.0.2.jar
D:\netwideo\restws\lib\jaxb-api-2.1. jar
D:\netwideo\restws\lib\wadl-cmdline.jar
D:\netwideo\restws\lib\http-20070405.jar
D:\netwideo\restws\lib\rome-0.9 .jar
D:\netwideo\restws\lib\activation-1.1.jar
D:\netwideo\restws\lib\jaxb-impl-2.1.10.jar
D:\net wideo\restws\lib\jersey-bundle-1.0.3.jar
D:\netwideo\restws\lib\jackson-lgpl-0.9.4.jar
D:\netwideo\restws\lib\FastInfoset- 1.2.2.jar
D:\netwideo\restws\lib\jaxb-xjc.jar
D:\IDEA8\lib\idea_rt.jar
2009-11-18 17:25:46 com. sun.jersey.api.core.ClasspathResourceConfig init
Information: Root resource classes found:
class example.HelloWorld
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Provider classes found:
RESTful WebService service has been started
Service access address: http://192.168.14.117:9999/helloworld

It can be seen that the service has started successfully, and the next step is to access the service from the browser Yes, enter http://192.168.14.117:9999/helloworld, an HTTP GET request will be sent, and see the result:
The test is successful!
——————–
There have been many packages since this project, some things Sun, some are not, here I put them all out, the entire project in the attachment, it can run under jdk5, 6.
Unfortunately, attachments can upload up to 6M things. This is more than 6M. I can only wait for 51cto to relax the upload file size. Each limit is best within 10M. —-Hope:), here I will list the packages used:
activation-1.1.jar asm-3.1.jar FastInfoset- 1.2.2.jar grizzly-servlet-webserver-1.8.6.4.jar http-20070405.jar jackson-lgpl-0.9.4.jar jaxb-api-2.1.jar jaxb-impl-2.1.10.jar jaxb-xjc. jar jdom-1.0.jar jersey-bundle-1.0.3.jar jettison-1.0.1.jar jsr311-api-1.0.jar mail-1.4.jar localizer.jar persistence-api-1.0.2.jar rome-0.9. jar stax-api-1.0-2.jar wadl-core.jar wadl-cmdline.jar wadl2java.jar

RESTful WebService is better than SOAP message-based WebService is much simpler and a lightweight Web service. RESTful WebService is stateless, and it is very easy to publish and call.

The following is a simple example of Hello World, in order to have a perceptual understanding of RESTful WebService. Because very professional and theoretical description of RESTful WebService is a painful thing to understand. Look at the examples to get a general idea, and then look at the theory and it will be easier to understand.

/**
* Getting started with RESTful WebService< br> * @author leizhimin 2009-11-18 16:42:43
*/

package example;
< br> import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;

//指定URI
@Path( “/helloworld”)
public class HelloWorld {
//Processing HTTP GET requests
@GET
// Processing requests The content format of the feedback is “text/plain”
@Produces( “text/plain”)
public String getClichedMessage() {
return “Hello World!” ;
        }

         public static void main(String[] args) throws IOException {
//Create a RESTful WebService
HttpServer server = HttpServerFactory.create( “http://192.168.14.117:9999/”);
//Start the service, which will cause a new thread to be opened
Server.start();
//Output some prompt messages of the service to the console
ln(System.out. style=”color:#800000″>”RESTful WebService service has started”);
System.out.println(“Service access address: http:< span style="color:#008000">//192.168.14.117:9999/helloworld”);
}
}

/**
* Getting started with RESTful WebService
* @author leizhimin 2009-11-18 16:42:43
*/

package example;

< span style="color:#0000ff">import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com. sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;

//指定URI
@Path( “/helloworld”)
public class< /span> HelloWorld {
//Handle HTTP GET requests
@GET
@GET
// The content format of processing request feedback is “text/plain”
@Produces( “text/plain”)
public String getClichedMessage( ) {
return “Hello World!”;
}
< br> public static void main(String[] args) throws IOException {
//Create RESTful WebService < br> HttpServer server = HttpServerFactory.create( “http://192.168.14.117:9999/”); < br> //Start the service, which will cause a new thread to be opened
serv er.start();
//Output some prompt information of the service to the console
System.out.println(“RESTful WebService has started”);
System.out.println(“Service access address: http://192.168.14.117:9999/helloworld”);
}
}

< span style="color:#008000">/**
* Getting started with RESTful WebService
* @author leizhimin 2009-11-18 16:42:43
*/
< span style="color:#0000ff">package example;

import com.sun.jersey.api.container.httpserver .HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
im port java.io.IOException;

//指定URI
@Path( “/helloworld”)
public class HelloWorld {< br> //Processing HTTP GET requests
@GET
// Processing request feedback content The format is “text/plain”
@Produces( “text/plain”)
public String getClichedMessage() {
return “Hello World!”;
        }

         public static void main(String[] args) throws IOException {
/ /Create RESTful WebService
HttpServer server = HttpServerFactory.create( “http://192.168.14.117:9999/”);
//Start the service, which will lead to the opening of a new thread
Server.start();
color style//Starting the service :#008000″>//Output some prompt messages of the service to the console
System.out.println(“RESTful WebService service has started”
>);
System.out.println(“Service access address: http://192.168.14.117:9999/helloworld “);
}
}

Run this class, the console input:

2009-11-18 17:25:37 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Scanning for root resource and provider classes in the paths:
D:\jdk16\jre\lib\alt-rt.jar
D:\jdk16\jre\lib\charsets.jar
D:\jdk16 \jre\lib\deploy.jar
D:\jdk16\jre\lib\javaws.jar
D:\jdk16\jre\lib\jce.jar
D:\jdk16\jre\lib\ jsse.jar
D:\jdk16\jre\lib\management-agent.jar
D:\jdk16\jre\lib\plugin.jar
D:\jdk16\jre\lib\resources.jar
D:\jdk16\jre\lib\rt.jar
D:\jdk16\jre\lib\ext\dnsns.jar
D:\jdk16\jre\lib\ext\localedata.jar < br> D:\jdk16\jre\lib\ext\sunjce_provider.jar
D:\jdk16\jre\lib\ext\sunmscapi.jar
D:\jdk16\jre\lib\ext\sunpkcs11.jar
D:\netwideo\restws\out\production\restws
D:\IDEA8\lib\javaee.jar
D:\netwideo\restws\lib\mail-1.4.jar
D: \netwideo\restws\lib\asm-3.1.jar
D:\netwideo\restws\lib\wadl2java.jar
D:\netwideo\restws\lib\jettison-1.0.1.jar
D:\netwideo\restws\lib\grizzly-servlet-webserver-1.8 .6.4.jar
D:\netwideo\restws\lib\wadl-core.jar
D:\netwideo\restws\lib\localizer.jar
D:\netwideo\restws\lib\jdom- 1.0.jar
D:\netwideo\restws\lib\jsr311-api-1.0.jar
D:\netwideo\restws\lib\stax-api-1.0-2.jar
D:\netwideo \restws\lib\persistence-api-1.0.2.jar
D:\netwideo\restws\lib\jaxb-api-2.1.jar
D:\netwideo\restws\lib\wadl-cmdline.jar
D:\netwideo\restws\lib\http-20070405.jar
D:\netwideo\restws\lib\rome-0.9.jar
D:\netwideo\restws\lib\activation-1.1. jar
D:\netwideo\restws\lib\jaxb-impl-2.1.10.jar
D:\netwideo\restws\lib\jersey-bundle-1.0.3.jar
D:\netwideo \restws\lib\jackson-lgpl-0.9.4.jar
D:\netwideo\restws\lib\FastInfoset-1.2.2.jar
D:\netwideo\restws\lib\jaxb-xjc.jar
D:\IDE A8\lib\idea_rt.jar
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Root resource classes found:
class example.HelloWorld
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Provider classes found:
RESTful WebService service has been started
Service access address: http://192.168.14.117:9999/helloworld

2009-11-18 17:25:37 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Scanning for root resource and provider classes in the paths:
D:\jdk16\jre\lib\alt-rt.jar
D:\jdk16\jre\lib\charsets.jar
D:\jdk16\jre\lib\deploy.jar
D:\jdk16\jre\lib\javaws.jar
D:\jdk16\jre\lib\jce.jar
D:\jdk16\jre\lib\jsse.jar
D: \jdk16\jre\lib\management-agent.jar
D:\jdk16\jre\lib\plugin.jar
D:\jdk16\jre\lib\resources.jar
D:\jdk16\ jre\li b\rt.jar
D:\jdk16\jre\lib\ext\dnsns.jar
D:\jdk16\jre\lib\ext\localedata.jar
D:\jdk16\jre\lib \ext\sunjce_provider.jar
D:\jdk16\jre\lib\ext\sunmscapi.jar
D:\jdk16\jre\lib\ext\sunpkcs11.jar
D:\netwideo\restws\ out\production\restws
D:\IDEA8\lib\javaee.jar
D:\netwideo\restws\lib\mail-1.4.jar
D:\netwideo\restws\lib\asm-3.1 .jar
D:\netwideo\restws\lib\wadl2java.jar
D:\netwideo\restws\lib\jettison-1.0.1.jar
D:\netwideo\restws\lib\grizzly- servlet-webserver-1.8.6.4.jar
D:\netwideo\restws\lib\wadl-core.jar
D:\netwideo\restws\lib\localizer.jar
D:\netwideo\restws \lib\jdom-1.0.jar
D:\netwideo\restws\lib\jsr311-api-1.0.jar
D:\netwideo\restws\lib\stax-api-1.0-2.jar
D:\netwideo\restws\lib\persistence-api-1.0.2.jar
D:\netwideo\restws\lib\jaxb-api-2.1.jar
D:\netwideo\restws\lib\ wadl-cmdline.jar
D:\netwideo\restws\lib\ht tp-20070405.jar
D:\netwideo\restws\lib\rome-0.9.jar
D:\netwideo\restws\lib\activation-1.1.jar
D:\netwideo\restws\lib \jaxb-impl-2.1.10.jar
D:\netwideo\restws\lib\jersey-bundle-1.0.3.jar
D:\netwideo\restws\lib\jackson-lgpl-0.9.4 .jar
D:\netwideo\restws\lib\FastInfoset-1.2.2.jar
D:\netwideo\restws\lib\jaxb-xjc.jar
D:\IDEA8\lib\idea_rt. jar
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Root resource classes found:
class example.HelloWorld
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
Information: Provider classes found:
RESTful WebService service has been started
Service access address: http://192.168.14.117:9999/helloworld

Visible, service The startup is successful, and the next step is to access the service from the browser. Enter http://192.168.14.117:9999/helloworld, and an HTTP GET request will be sent to see the result:

< p>

The test was successful!

——————–

There have been many packages since this project, some things Sun, some are not, here I put them all out, the entire project in the attachment, it can run under jdk5, 6.

Unfortunately, attachments can upload things up to 6M. This is more than 6M. I can only wait for 51cto to relax the upload file size. Each limit is best within 10M. —-Hope:), here I will list the packages used:

activation-1.1.jar asm-3.1.jar FastInfoset- 1.2.2.jar grizzly-servlet-webserver-1.8.6.4.jar http-20070405.jar jackson-lgpl-0.9.4.jar jaxb-api-2.1.jar jaxb-impl-2.1.10.jar jaxb-xjc. jar jdom-1.0.jar jersey-bundle-1.0.3.jar jettison-1.0.1.jar jsr311-api-1.0.jar mail-1.4.jar localizer.jar persistence-api-1.0.2.jar rome-0.9. jar stax-api-1.0-2.jar wadl-core.jar wadl-cmdline.jar wadl2java.jar

activation-1.1.jar asm- 3.1.jar FastInfoset-1.2.2.jar grizzly-servlet-webserver-1.8.6.4.jar http-20070405.jar jackson-lgpl-0.9.4.jar jaxb-api-2.1.jar jaxb-impl-2.1.10. jar jaxb-xjc.jar jdom-1.0.jar jersey-bundle-1.0.3.jar jettison-1.0.1.jar jsr311-api-1.0.jar mail-1.4.jar localizer.jar persistence-api-1.0.2. jar rome-0.9.jar stax-api-1.0-2.jar wadl-core.jar wadl-cmdline.jar wadl2java.jar

activation-1.1.jar asm-3.1.jar FastInfoset -1.2.2.jar grizzly-servlet-webserver-1.8.6.4.jar http-20070405.jar jackson-lgpl-0.9.4.jar jaxb-api-2.1.jar jaxb-impl-2.1.10.jar jaxb-xjc .jar jdom-1.0.jar jersey-bundle-1.0.3.jar jettison-1.0.1.jar jsr311-api-1.0.jar mail-1.4.jar localizer.jar persistence-api-1.0.2.jar rome-0.9 .jar stax-api-1.0-2.jar wadl-core.jar wadl-cmdline.jar wadl2java.jar

Leave a Comment

Your email address will not be published.