CXF (2.7.10)-RESTful Services

Source: Internet
Author: User

1. Define JavaBean. Note @XmlRootElement annotations that map JavaBean to XML elements.

 PackageCom.huey.demo.bean;Importjavax.xml.bind.annotation.XmlRootElement;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor@xmlrootelement Public classBook {PrivateString title; PrivateString author; PrivateString Publisher; PrivateString ISBN;}
 Package Com.huey.demo.bean; Import javax.xml.bind.annotation.XmlRootElement; Import Lombok. Allargsconstructor; Import Lombok. Data; Import Lombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor@xmlrootelementPublicclass  resultmsg {    private  String resultcode;     Private String message;    

2. Define the service interface. Note the role of the individual annotations.

 Packagecom.huey.demo.ws;Importjava.util.List;ImportJavax.jws.WebService;ImportJavax.ws.rs.DELETE;ImportJavax.ws.rs.GET;ImportJavax.ws.rs.POST;ImportJavax.ws.rs.PUT;ImportJavax.ws.rs.Path;ImportJavax.ws.rs.PathParam;Importjavax.ws.rs.Produces;ImportJavax.ws.rs.core.MediaType;ImportCom.huey.demo.bean.Book;Importcom.huey.demo.bean.ResultMsg; @WebService Public InterfaceBookservice {@GET//Specify the request method@Path ("/BOOK/{ISBN}")//Specifies the URI of the resource@Produces ({mediatype.application_xml})//Specify the media type for request/response     PublicBook GetBook (@PathParam ("ISBN") String ISBN); @GET @Path ("/books") @Produces ({mediatype.application_xml}) PublicList<book>Getbooks (); @POST @Path ("/book") @Produces ({mediatype.application_xml}) Publicresultmsg addbook (book book); @PUT @Path ("/BOOK/{ISBN}") @Produces ({mediatype.application_xml}) PublicResultmsg Updatebook (@PathParam ("ISBN") String ISBN, Book book); @DELETE @Path ("/BOOK/{ISBN}") @Produces ({mediatype.application_xml}) PublicResultmsg Deletebook (@PathParam ("ISBN") String ISBN);}

3. Implement the Service interface.

 PackageCom.huey.demo.ws.impl;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.jws.WebService;Importorg.apache.commons.lang.StringUtils;ImportCom.huey.demo.bean.Book;ImportCOM.HUEY.DEMO.BEAN.RESULTMSG;ImportCom.huey.demo.ws.BookService;Importcom.sun.org.apache.commons.beanutils.BeanUtils; @WebService Public classBookserviceimplImplementsBookservice {List<Book> books =NewArraylist<book>();  PublicBookserviceimpl () {Books.add (NewBook ("The dedication of the suspect X", "The east of the GUI Wu", "Nanhai Publishing Company", "9787544245555")); Books.add (NewBook ("Kite-chasing", "Husseini", "Shanghai People's publishing house", "9787208061644")); Books.add (NewBook ("See", "Chai Jing", "Guangxi Normal University Press", "9787549529322")); Books.add (NewBook ("White Nights", "Dong Ye gui wu", "Nanhai Publishing Company", "9787544242516")); }             PublicBook GetBook (String ISBN) { for(book book:books) {if(BOOK.GETISBN (). Equals (ISBN)) {returnBook ; }        }        return NULL; }         PublicList<book>Getbooks () {returnBooks; }         Publicresultmsg addbook (book book) {if(Book = =NULL||Stringutils.isempty (BOOK.GETISBN ())) {            return NewResultmsg ("Fail", "parameter is incorrect!") "); }        if(GetBook (BOOK.GETISBN ())! =NULL) {            return NewResultmsg ("FAIL", "the book already exists!") ");        } books.add (book); return NewResultmsg ("SUCCESS", "Add success!") "); }     Publicresultmsg Updatebook (String ISBN, Book book) {Book Target=GetBook (ISBN); if(Target! =NULL) {resultmsg resultmsg=NewResultmsg ("SUCCESS", "Update success!") "); Try{beanutils.copyproperties (target, book); } Catch(Exception e) {resultmsg=NewResultmsg ("FAIL", "Unknown Error!") "); }            returnresultmsg; }        return NewResultmsg ("Fail", "the book does not exist!") "); }         Publicresultmsg Deletebook (String ISBN) { Book Book=GetBook (ISBN); if(Book! =NULL) {books.remove (book); return NewResultmsg ("SUCCESS", "Delete succeeded!") "); }        return NewResultmsg ("Fail", "the book does not exist!") "); }}

4. Spring configuration.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"Xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"XMLNS:CXF= "Http://cxf.apache.org/core"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd Http://cxf.apache.org/core Http://cxf.apa Che.org/schemas/core.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">    <BeanID= "Bookrestservice"class= "Com.huey.demo.ws.impl.BookServiceImpl" />    <Jaxrs:serverID= "Bookservice"Address= "/rest">        <Jaxrs:servicebeans>            <refBean= "Bookrestservice" />        </Jaxrs:servicebeans>    </Jaxrs:server></Beans>

5. Web. XML configuration.

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <welcome-file-list>        <Welcome-file>index.jsp</Welcome-file>    </welcome-file-list>    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:applicationContext.xml</Param-value>    </Context-param>    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <servlet>        <Servlet-name>Cxfservlet</Servlet-name>        <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Cxfservlet</Servlet-name>        <Url-pattern>/ws/*</Url-pattern>    </servlet-mapping></Web-app>

6. Start Tomcat to run the Web project.

7. Test.

A) Getbooks

b) GetBook

c) Addbook

D) Updatebook

e) Deletebook

CXF (2.7.10)-RESTful Services

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.