Getting Started with JAVA RESTful Web Services-jersey

Source: Internet
Author: User
Tags glassfish

Create a new project:

Using Maven & Eclipse (Mars)

Maven command line to create a new project (MAVEN environment build if you don't know what you're checking out)

MVN archetype:generate-darchetypeartifactid=jersey-quickstart-grizzly2-darchetypegroupid= Org.glassfish.jersey.archetypes-dinteractivemode=false-dgroupid=com.example-dartifactid=simple-service- dpackage=com.example-darchetypeversion=2.14


This is to download some packages patiently waiting ...

After processing, you will find that Simple-service has created


Import into Eclipse, (Eclipse to install Maven plugin, how to install, check it yourself)

The directory structure after successful import is as follows


Open the Main.java under the package com.example

Package Com.example;import Org.glassfish.grizzly.http.server.httpserver;import Org.glassfish.jersey.grizzly2.httpserver.grizzlyhttpserverfactory;import Org.glassfish.jersey.server.resourceconfig;import Java.io.ioexception;import java.net.URI;/** * Main class. * */public class Main {//Base URI the Grizzly HTTP server would listen on public static final String Base_uri = "HT    tp://localhost:8080/myapp/";     /** * starts Grizzly HTTP Server exposing JAX-RS resources defined in this application.     * @return Grizzly HTTP server. */public static Httpserver StartServer () {//Create a resource config, scans for jax-rs resources and Prov        Iders//In Com.example package final ResourceConfig rc = new ResourceConfig (). Packages ("Com.example");        Create and start a new instance of Grizzly HTTP Server//exposing the Jersey application at Base_uri    Return Grizzlyhttpserverfactory.createhttpserver (Uri.create (Base_uri), RC);  }    /**   * Main method.  * @param args * @throws IOException */public static void main (string[] args) throws IOException {final        Httpserver Server = StartServer (); System.out.println (String.Format ("Jersey app started with WADL available at" + "%sapplication.wadl\nhit en        ter to stop it ... ", Base_uri));        System.in.read ();    Server.stop (); }}

Direct run as Java application, do not put the project on the Web server (⊙o⊙) ...

After running, the console outputs the following information:


Follow the prompts we entered in the browser Http://localhost:8080/myapp/application.wadl, the miracle appeared, as shown in (⊙o⊙) ...


The next hint appears http://localhost:8080/myapp/myresource, returning a string of textual information. Such as


Why is this access path, and why return to got it! What the hell is going on here. I don't know anything!

Com.example also has a file, Myresource.java, open it up and see.

Package Com.example;import Javax.ws.rs.get;import Javax.ws.rs.path;import javax.ws.rs.produces;import javax.ws.rs.core.mediatype;/** * Root resource (exposed at "myresource" path) */@Path ("MyResource") public class MyResource {    /**     * Method handling HTTP GET requests. The returned object is sent     * to the client as "Text/plain" media type.     *     * @return String that would be returned as a text/plain response.     */    @GET    @Produces (mediatype.text_plain) public    String Getit () {        return "Got it!";    }}
Seems to have a little connection with this, in the end what is going on, next time again to discuss Ah ...

command line that sentence, hit Enter to stop it ... Ok,stop it.

Mission completed!
O (∩_∩) o~

Getting Started with JAVA RESTful Web Services-jersey

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.