A restful form of webservices implemented by Apache CXF

Source: Internet
Author: User
Tags soap

I said in the previous article (HTTP://WWW.ZIFANGSKY.CN/2016/03/APACHE-CXF implementation of the soap form of webservices/) how to implement a SOAP-style Web service, This article will continue to explain how to implement restful forms of Web service


(1) Project structure:

Here, I did not create a new project, but on the basis of the previous project, specifically to create a new 3 files, as shown in:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/E3/wKiom1cLoYTQuEiOAABKJXINyo0227.png "title=" 20160315164012_42269.png "alt=" Wkiom1cloytqueioaabkjxinyo0227.png "/>

Where the user class is a common entity class, the Restservice and Restserviceimpl classes represent the service interface and its implementation class, respectively.

(2) User.java:

package cn.zifangsky.entity;import  Javax.xml.bind.annotation.XmlRootElement, @XmlRootElement (name= "UserInfo") public class user { private int id;private string name;private string contact;public int  GetId ()  {return id;} Public void setid (Int id)  {this.id = id;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public string getcontact ()  {return contact;} Public void setcontact (String contact)  {this.contact = contact;}} 

(3) restservice.java:

Package Cn.zifangsky.service;import Javax.ws.rs.get;import Javax.ws.rs.path;import javax.ws.rs.pathparam;import Javax.ws.rs.produces;import Javax.ws.rs.core.mediatype;import Cn.zifangsky.entity.user;public Interface Restservice {@GET @path (value= "/user/{id}") @Produces (value = {mediatype.application_xml, Mediatype.application_json }) Public User GetUser (@PathParam ("id") int id);}

(4)Restserviceimpl. Java:

Package Cn.zifangsky.service.impl;import Cn.zifangsky.entity.user;import Cn.zifangsky.service.restservice;public Class Restserviceimpl implements Restservice {public user getUser (int id) {User user = new User (); User.setid (ID); USER.SETN Ame ("Zifangsky"); User.setcontact ("http://www.zifangsky.cn"); return user;}}

(5) Web. XML does not change, Modify service-beans.xml:

Add <jaxrs:server></jaxrs:server> node, indicates a restful service

<jaxrs:server id= "Userrest"  address= "/services/rest" >< jaxrs:servicebeans><bean class= "Cn.zifangsky.service.impl.RestServiceImpl" &NBSP;/></JAXRS: servicebeans><jaxrs:extensionmappings><entry key= "JSON"  value= "Application/json"  / ><entry key= "xml"  value= "Application/xml"  /></jaxrs:extensionmappings></jaxrs : Server> 

after adding, service-beans.xml The complete code for this file is:

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "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:soap=" Http://cxf.apache.org/bindings/soap "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/jaxrs    http://cxf.apache.org/schemas/ jaxrs.xsd  "><bean id=" Outlogginginterceptor " class=" Org.apache.cxf.interceptor.LoggingOutInterceptor " /><bean id=" Loggingfeature " class=" Org.apache.cxf.feature.LoggingFeature " /><bean id=" InloggiNginterceptor " class=" Org.apache.cxf.interceptor.LoggingInInterceptor " /><jaxws:server id = "Sayhelloservices"  serviceclass= "Cn.zifangsky.service.CXFService"  address= "/services/soap"   ><jaxws:servicebean><bean class= "Cn.zifangsky.service.impl.CXFServiceImpl"  /></ Jaxws:servicebean><jaxws:outinterceptors><ref bean= "Outlogginginterceptor"  /></ Jaxws:outinterceptors><jaxws:ininterceptors><ref bean= "Inlogginginterceptor"  /></ Jaxws:ininterceptors><jaxws:features><ref bean= "Loggingfeature"  /><wsa:addressing  xmlns:wsa= "Http://cxf.apache.org/ws/addressing"  /></jaxws:features></jaxws:server> <jaxrs:server id= "Userrest"  address= "/services/rest" ><jaxrs:serviceBeans><bean  class= "Cn.zifangsky.service.impl.RestServiceImpl" &NBSP;/&GT;&LT;/JAXRS:SERVICEBEANS&GT;&LT;JAXRS: Extensionmappings><entry key= "JSON"  value= "Application/json"  /><entry key= "xml"  value= "Application/xml"  / ></jaxrs:extensionMappings></jaxrs:server></beans>

(6) Test:

The entire configuration has been completed here, starting the project in Tomcat, the effect is this:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/E3/wKiom1cLolTTMtpyAAA8xO5An5M413.png "title=" 20160315164913_42997.png "alt=" Wkiom1clolttmtpyaaa8xo5an5m413.png "/>

Access in the browser: HTTP://LOCALHOST:8080/CXFDEMO/SERVICES/REST/USER/9

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/E3/wKiom1cLom-Cpgi_AAA2aGvnaFk242.png "title=" 20160315165045_63060.png "alt=" Wkiom1clom-cpgi_aaa2agvnafk242.png "/>

As you can see, the result is correct, to this end of all configurations


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1762762

A restful form of webservices implemented by Apache CXF

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.