Not much to say, first look at specific examples:
File directory structure:
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app 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 "version=" 2.5 "><display-name>platform</display-name><!--Spring ApplicationContext the path to the configuration file, you can use wildcards, multiple paths with the, number delimited this parameter is used for the following spring Context Loader--><context-param><param-name >contextconfiglocation</param-name><param-value>classpath:/applicationcontext.xml,classpath:/ applicationcontext-rs.xml</param-value></context-param><!--Character Encoding filter-->< Filter><filter-name>encodingfilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>utf-8</param-value></init-param></filter> <filter-mapping><filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> <listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class ></listener><!--Spring Refresh introspector prevent memory leaks--><listener><listener-class> org.springframework.web.util.introspectorcleanuplistener</listener-class></listener><!--Apache CXF Servlet Configuration--><servlet><servlet-name>cxfservlet</servlet-name><servlet-class> org.apache.cxf.transport.servlet.cxfservlet</servlet-class></servlet><servlet-mapping>< servlet-name>cxfservlet</servlet-name><url-pattern>/rest/*</url-pattern></ Servlet-mapping></web-app>
Spring's Xml-applicationcontext.xml
<?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:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd Http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-3.2.xsd Http://www.springframework.org/schema/jee/HTTP Www.springframework.org/schema/jee/spring-jee-3.2.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.2.xsd "default-lazy-init=" true "><description> Spring Public Profile </description><!--automatically registers the bean with annotation and guarantees @required that the properties of the @Autowired are injected--><context: Component-scan BASE-package= "com.zd.daoimpl.*,com.zd.beanimpl.*,com.zd.serviceimpl.*,com.zd.service.*" ></context: Component-scan><bean id= "Dozermapper" class= "Org.dozer.DozerBeanMapper" lazy-init= "false"/><!--Matrix BPM--><bean id= "Transactioninterceptor" class= " Org.springframework.transaction.interceptor.TransactionInterceptor "><property name=" TransactionManager " ><ref bean= "TransactionManager"/></property><property name= "Transactionattributes" >< Props><prop key= "*" >propagation_required,-exception</prop></props></property></ Bean></beans>
The xml-of the restfulApplicationcontext-rs.xml
<?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:jaxrs= "Http://cxf.apache.org/jaxrs" xmlns:context= "http// Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://cxf.apache.org/jaxrs http://cxf.apache.org/ Schemas/jaxrs.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd all restful service configurations for "><description>rails CM" </description><context:component-scan Base-package= "Com.zd"/><bean id= "Jsonprovider" class= "Org.codehaus.jackson.jaxrs.JacksonJsonProvider" > </bean><jaxrs:server id= "Restcontainer" address= "/" ><JAXRS:ININTERCEPTORS></JAXRS: Ininterceptors><jaxrs:servicebeans><ref bean= "Userinfoserviceimpl"/></jaxrs:serviCebeans></jaxrs:server></beans>
Entity:
Package Com.zd.entity;import java.io.serializable;/** System User Table **/public class UserInfo implements Serializable {private String Id;private string name;//user name private string sex;//Gender private string idno;//ID number private string account;//account privat E string pwd;//Password public string getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getaccount () {return account;} public void Setaccount (String account) {this.account = account;} Public String getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String Getidno () {return idno;} public void Setidno (String idno) {this.idno = Idno;}}
Build the XML entity:
Package Com.zd.dtd;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.xmlaccessortype;import Javax.xml.bind.annotation.xmlelement;import Javax.xml.bind.annotation.xmlrootelement;import Javax.xml.bind.annotation.XmlType; @XmlRootElement (name= " Userinfodto ") @XmlAccessorType (xmlaccesstype.property) @XmlTypepublic class Userinfodtd {private String id;private String name;//User name private string sex;//Gender private string idno;//ID number private string account;//account private string pwd;//password @XmlElement (name= "id", required=false) public String getId () {return ID;} public void SetId (String id) {this.id = ID;} @XmlElement (name= "name", Required=false) public String getName () {return name;} public void SetName (String name) {this.name = name;} @XmlElement (name= "account", Required=false) public String Getaccount () {return account;} public void Setaccount (String account) {this.account = account;} @XmlElement (name= "Sex", required=false) public String getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} @XmlElement (name= "Sex", required=false) public String Getidno () {return idno;} public void Setidno (String idno) {this.idno = Idno;} @XmlElement (name= "Sex", required=false) public String getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;}}
Interface Service Layer:
Package Com.zd.service;import Javax.ws.rs.get;import Javax.ws.rs.path;import javax.ws.rs.produces;import Javax.ws.rs.queryparam;import javax.ws.rs.core.mediatype;import javax.ws.rs.core.Response; @Path ("/userinfo") Public interface Userinfoservice {/** * Gets user information based on ID * @param ID User ID * @return JSON data */@GET @path ("/getuserinfo") @Produces ( {"Text/html;charset=utf-8", mediatype.text_html}) Public Response Getbasicuserinfo (@QueryParam ("id") String ID);
Service Implementation layer:
Package Com.zd.serviceimpl;import Javax.ws.rs.core.Response; Import Net.sf.json.jsonarray;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Com.zd.beanimpl.userbeanimpl;import Com.zd.dtd.UserInfoDTD; Import Com.zd.entity.userinfo;import Com.zd.service.UserinfoService; @Componentpublic class Userinfoserviceimpl implements Userinfoservice {@Autowired (required=true) Private Userbeanimpl Userbeanimpl; @Overridepublic Response getbasicuserinfo (String id) {UserInfo UserInfo = userbeanimpl.getuserinfo (ID); USERINFODTD userdtd=new userinfodtd (); Userdtd.setsex (Userinfo.getsex ()); Userdtd.setid (UserInfo.getId ()); Userdtd.setidno (Userinfo.getidno ()); Userdtd.setname (Userinfo.getname ()); Userdtd.setpwd (USERINFO.GETPWD ()); Userdtd.setaccount (Userinfo.getaccount ());//json conversion Jsonarray Jsonarray = Jsonarray.fromobject (userInfo); return Response.ok (Jsonarray.tostring ()). build ();}}
Servicebean:
Package Com.zd.beanimpl;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.zd.daoimpl.userinfodaoimpl;import com.zd.entity.UserInfo; @Service//By default all functions in a class are included in transaction management. @Transactional Public Class Userbeanimpl {@Autowired (required=true) Private Userinfodaoimpl userinfodaoimpl;public UserInfo GetUserInfo ( String ID) {UserInfo userinfo=userinfodaoimpl.getuserinfo (ID); return UserInfo;}}
Dao:
Package Com.zd.daoimpl;import Org.springframework.stereotype.repository;import Org.springframework.transaction.annotation.transactional;import com.zd.entity.UserInfo; @Repository// By default, all functions in a class are included in transaction management. @Transactionalpublic class Userinfodaoimpl {public UserInfo getuserinfo (String id) {UserInfo UserInfo =new UserInfo (); Userinfo.setaccount ("Zhudan"); Userinfo.setid ("1111"); Userinfo.setid ("520911188119186770"); Userinfo.setname ("Zhudan"); Userinfo.setpwd ("1221"); Userinfo.setsex ("female"); return userInfo;}}
Page Access Address:
Http://localhost:8080/restful/rest/Userinfo/getUserinfo
Page Access results:
Summarize:
First, by this example, the macro understanding of RESTful service building, restful details in the future use of deep understanding
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cxf+spring+jaxb+json Building RESTful Services