Jersey (1.19.1)-Hello World

Source: Internet
Author: User

1. Maven Dependency

<Properties>    <jersey.version>1.19.1</jersey.version></Properties><Dependencies>    <Dependency>        <groupId>Com.sun.jersey</groupId>        <Artifactid>Jersey-core</Artifactid>        <version>${jersey.version}</version>    </Dependency>    <Dependency>        <groupId>Com.sun.jersey</groupId>        <Artifactid>Jersey-server</Artifactid>        <version>${jersey.version}</version>    </Dependency>    <Dependency>        <groupId>Com.sun.jersey</groupId>        <Artifactid>Jersey-servlet</Artifactid>        <version>${jersey.version}</version>    </Dependency>    <Dependency>        <groupId>Com.sun.jersey</groupId>        <Artifactid>Jersey-json</Artifactid>        <version>${jersey.version}</version>    </Dependency>    <Dependency>        <groupId>Com.sun.jersey</groupId>        <Artifactid>Jersey-client</Artifactid>        <version>${jersey.version}</version>    </Dependency>    <Dependency>        <groupId>Org.projectlombok</groupId>        <Artifactid>Lombok</Artifactid>        <version>1.16.4</version>    </Dependency></Dependencies>
View Code

2. JavaBean

 PackageCom.huey.hello.jersey.bean;Importjavax.xml.bind.annotation.XmlRootElement;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor, @NoArgsConstructor @allargsconstructor@data@xmlrootelement Public classBook {PrivateString title; Privatestring[] Author; PrivateString Publisher; Private DoublePrice ; PrivateString ISBN; }

3. RESTful Web Services

 PackageCom.huey.hello.jersey.service;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.ws.rs.Consumes;ImportJavax.ws.rs.GET;ImportJavax.ws.rs.POST;ImportJavax.ws.rs.Path;ImportJavax.ws.rs.PathParam;Importjavax.ws.rs.Produces;Importjavax.ws.rs.WebApplicationException;ImportJavax.ws.rs.core.MediaType;ImportCom.huey.hello.jersey.bean.Book; @Path ("/books") Public classBookservice {Private StaticList<book>Books; Static{Books=NewArraylist<book>(); Books.add (NewBook ("High-performance MySQL",                 Newstring[]{"Baron Schwartz", "Peter Zaitsev"},                "Electronic Industry Press",                 128.00,                "9787121198854")); Books.add (NewBook ("http Authoritative guide",                 Newstring[]{"David Gourley", "Brian totty"},                "People's post and Telecommunications press",                 109.00,                "9787115281487")); } @GET @Produces ({mediatype.application_json, mediatype.application_xml}) PublicList<book>Getbooks () {returnBooks; } @GET @Path ("/{ISBN}") @Produces ({Mediatype.application_json, mediatype.application_xml}) PublicBook GetBook (@PathParam ("ISBN") String ISBN) { for(book book:books) {if(BOOK.GETISBN (). Equals (ISBN)) {returnBook ; }        }        Throw NewWebapplicationexception (404); } @POST @Consumes ({mediatype.application_json, mediatype.application_xml}) Public voidAddbook {books.add (book); }    }

4. Web. xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version= "2.5">    <Display-name>Hello-jersey</Display-name>    <welcome-file-list>        <Welcome-file>Index.html</Welcome-file>    </welcome-file-list>    <servlet>        <Servlet-name>Jerseyrestservice</Servlet-name>        <Servlet-class>Com.sun.jersey.spi.container.servlet.ServletContainer</Servlet-class>        <Init-param>            <Param-name>Com.sun.jersey.config.property.packages</Param-name>            <Param-value>Com.huey.hello.jersey.service</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Jerseyrestservice</Servlet-name>        <Url-pattern>/rest/*</Url-pattern>    </servlet-mapping></Web-app>

5. Deploy Start Web project.

6. Test.

Jersey (1.19.1)-Hello World

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.