Getting Started with RESTful WebService

Source: Internet
Author: User

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://lavasoft.blog.51cto.com/62575/229206

RESTful WebService is a lightweight Web service that is simpler than SOAP message-based webservice, and restful webservice is stateless, publishing and calling are very easy and easy.

Here is one of the simplest examples of Hello world to have a perceptual knowledge of restful webservice. Because a very professional and theoretical contempt for restful webservice is a painful thing to understand. Take a look at an example to understand.

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

ImportCom.sun.jersey.api.container.httpserver.HttpServerFactory;
ImportCom.sun.net.httpserver.HttpServer;

ImportJavax.ws.rs.GET;
ImportJavax.ws.rs.Path;
ImportJavax.ws.rs.Produces;
ImportJava.io.IOException;

//Specify URI
@Path ("/helloworld")
Public classHelloWorld {
//Handling Get requests for HTTP
@GET
//The content format for processing request feedback is "Text/plain"
@Produces ("Text/plain")
PublicString Getclichedmessage () {
return "Hello world!";
}

Public Static voidMain (string[] args)throwsIOException {
//Create RESTful WebService service
Httpserver Server = Httpserverfactory.create ( "Http://192.168.14.117:9999/");
                //start the service, which causes a new thread to be opened
                 Server.start ();
                //Some tips for the output 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 ");
       }
}

To run this class, console output:

2009-11-18 17:25:37 com.sun.jersey.api.core.ClasspathResourceConfig Init
Information: Scanning forRoot 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
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:\IDEA8\lib\idea_rt.jar
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig Init
Info: Root Resource classes found:
classExample. 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

Service started successfully, down is to access the service from the browser, enter Http://192.168.14.117:9999/helloworld, will send an HTTP GET request to see the results:

All of the jar 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

This article is from the lava blog, so be sure to keep this source http://lavasoft.blog.51cto.com/62575/229206

RESTful WebService Introduction (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.