Building restful services with Jersey 1--helloworld

Source: Internet
Author: User
Tags glassfish

This article specific code implementation reference from: http://blog.csdn.net/kkkloveyou/article/details/21391033, if there is a problem, please contact me.

This article just made a little supplement to the original text, thanks to the original author's wonderful article, I helped a lot.

One, the environment

Ide:myeclipse10

Version of Tomcat, the JDK version will be detailed in the next article.

Second, the construction of the project

In https://jersey.java.net/download.html, two very useful mvn instructions can be found:

Command one: Build a project based on the Servlet container:

MVN archetype:generate-darchetypegroupid=org.glassfish.jersey.archetypes-darchetypeartifactid= jersey-quickstart-webapp-darchetypeversion=2.23.1

darchetypeversion=2.23.1 indicates that the version of Jersey used is 2.23.1

When using this directive, please be patient and be sure to fill in the necessary information as the connection speed is slow.

The Pom.xml in the project created with this directive has added its own dependency on jersey and does not need to be added manually.

The successfully created project is imported into Myeclipse10, and the project created here is under the F-disk, using Maven4myeclipse to update the engineering structure and update the dependencies. The engineering structure is as follows:

  

The Myresource.java file was created by Maven itself and reads as follows:

1  Packagecom.wudi.test;2 3 ImportJavax.ws.rs.GET;4 ImportJavax.ws.rs.Path;5 Importjavax.ws.rs.Produces;6 ImportJavax.ws.rs.core.MediaType;7 8 /**9 * Root resource (exposed at "myresource" path)Ten  */ One@Path ("MyResource") A  Public classMyResource { -  -     /** the * Method handling HTTP GET requests. The returned object would be sent - * to the client as "Text/plain" media type. -      * -      * @returnString that would be returned as a text/plain response. +      */ - @GET + @Produces (Mediatype.text_plain) A      PublicString Getit () { at         return"Got It!"; -     } -}

Command two: Build a project based on the Grizzly 2 HTTP server container:

MVN archetype:generate-darchetypegroupid=org.glassfish.jersey.archetypes-darchetypeartifactid= jersey-quickstart-grizzly2-darchetypeversion=2.23.1

This command constructs the project, has not contacted, does not make the description.

Third, the specific code implementation

Make a little change to the Web. XML for the auto-generated project, as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "3.0" 3 xmlns= "Http://java.sun.com/xml/ns/javaee" 4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">7   <Display-name></Display-name>    8   <welcome-file-list>9     <Welcome-file>index.jsp</Welcome-file>Ten   </welcome-file-list>    One     <servlet> A         <Servlet-name>Jersey WEB Application</Servlet-name> -         <Servlet-class>Org.glassfish.jersey.servlet.ServletContainer</Servlet-class> -         <Init-param> the             <Param-name>Jersey.config.server.provider.packages</Param-name> -             <Param-value>Com.wudi.test</Param-value> -         </Init-param> -         <Load-on-startup>1</Load-on-startup> +     </servlet> -     <servlet-mapping> +         <Servlet-name>Jersey WEB Application</Servlet-name> A         <Url-pattern>/webapi/*</Url-pattern> at     </servlet-mapping> - </Web-app>

Deploy to Tomcat, start the server, Url:http://localhost:8080/jerseydemo/webapi/myresource

Then write your own two demo, the code is as follows:

1  Packagecom.wudi.test;2 3 ImportJavax.ws.rs.GET; 4 ImportJavax.ws.rs.Path; 5 Importjavax.ws.rs.Produces; 6 ImportJavax.ws.rs.PathParam; 7 ImportJavax.ws.rs.core.MediaType; 8   9   Ten@Path ("/hello")   One  Public classHelloresource { A @GET - @Produces (Mediatype.text_plain) -      PublicString SayHello () { the         return"Hello world!" ;  -     }   -     -        + @GET -@Path ("/{param}")     +@Produces ("Text/plain;charset=utf-8")   A      PublicString sayHelloToUTF8 (@PathParam ("param") String username) { at         return"Hello" +username;  -     }   -        -}

Http://localhost:8080/jerseyDemo/webapi/hello

Http://localhost:8080/jerseyDemo/webapi/hello/Way, how you doing?

  

Building restful services with Jersey 1--helloworld

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.