CXF with spring releases WS services with SOAP services, RESTful services
1. Visit: http://localhost:8088/sniperWS/services/
See what services are available, including Available SOAP services, Available RESTful Services
2. Client calls RESTful services:
Http://localhost:8088/sniperWS/services/address/getSuggestions.query
Call Example:
$.ajax ({
URL: "Http://ip:port/sniperWS/services/address/getSuggestions.query",
dataType: "JSON",
data: {
"address": $ ("#address"). Val ( ),
Max: ten,
"_type": "JSON"
},
cache:false,
success: function (data) {
response ($.map (DATA.ADDRESSVO, function (item) {
return {
label:item.address,
value:item.address
}
}));
},
error:function (suggestionrequest, Textstatus, error) {
alert (Error);
}
});
Ws-services-context.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:context=" Http://www.springframework.org/schema/context " xmlns:jaxws="/HTTP/ Cxf.apache.org/jaxws " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:jaxrs="/HTTP/ Cxf.apache.org/jaxrs " xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:// www.springframework.org/schema/context http://www.springframework.org/schema/context/ spring-context-3.0.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 " default-autowire= "ByName" > <!--Spring Publishing Web service configuration --> <import resource= " Classpath:meta-inf/cxf/cxf.xml " /> <import resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" /> <import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml" /><bean id= "AddressService" class = "Cn.sniper.ws.service.AddressServiceImpl" /><!-- mode one: Release soap services --><!-- <jaxws:endpoint id= "Addressws" implementor= "#addressService" address= "/addressws" /> --><!-- Way II: Release soap services --><jaxws:server id= "Addressws" address= "/addressws" > <jaxws:serviceBean> <ref bean= "Addressservice" /> </jaxws:serviceBean> <!-- <jaxws:ininterceptors> <bean class= " Cn.sniper.ws.service.LoggingInterceptor "></BEAN>&NBSP;&Nbsp;</jaxws:ininterceptors> --></jaxws:server><!-- Release restful services--> <jaxrs:server id= "Addressservicews" address= "/address" > <jaxrs:extensionMappings> <entry key= "JSON" value= "Application/json" /> <entry key= " XML " value=" Application/xml " /> </jaxrs:extensionMappings> <jaxrs:servicebeans> <ref bean= " Addressservice " /> </jaxrs:serviceBeans> <!-- <jaxrs:ininterceptors> <bean class= " Cn.sniper.ws.service.LoggingInterceptor "></bean> </jaxrs:ininterceptors> --></ Jaxrs:server></beans>
Beans.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= xmlns:xsi= xmlns:context= xmlns:aop= xmlns:tx= xsi: schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/aop/spring-ao P-3.0.xsd "> <context:component-scan base-package=" Cn.sniper.ws.service "></context:component-scan> <import resource= "Ws-services-context.xml"/></beans>
Addressservice
Package Cn.sniper.ws.service;import Java.util.list;import Javax.jws.webparam;import javax.jws.webservice;import Javax.jws.soap.soapbinding;import javax.jws.soap.soapbinding.style;import cn.sniper.ws.po.Address; @SOAPBinding ( style = Style.document) @WebService (targetnamespace = "http://cn.sniper.ws.service") public interface Addressservice { Public list<address> getsuggestions (@WebParam (name= "address") String Address, @WebParam (name= "max") int max); }
Addressserviceimpl
package cn.sniper.ws.service;import java.util.arraylist;import java.util.list;import javax.jws.webservice;import javax.jws.soap.soapbinding;import javax.jws.soap.soapbinding.style; import javax.ws.rs.get;import javax.ws.rs.path;import javax.ws.rs.queryparam;import Cn.sniper.ws.po.Address, @Path (value= "/") @SOAPBinding (style = style.document) @WebService ( servicename = "Address", portname = "Address", targetNamespace = "Http://cn.sniper.ws.service", endpointinterface = "Cn.sniper.ws.service.AddressService") Public class addressserviceimpl implements AddressService { @GET @Path ("Getsuggestions.query ") public list<address> getsuggestions (@QueryParam (" Address ") string address, @ Queryparam ("Max") Int max) { list<address> addresslist = new arraylist<address> (); Addresslist.add (new address (1l, "admin-1")) addresslist.add (New address (2L, ) Admin-2 ")); return addresslist; } }
Address
package cn.sniper.ws.po;import java.io.serializable;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;/** * Note that the name of the @XmlRootElement (name= "ADDRESSVO") does not duplicate the name of the property, * otherwise the XML parsing process error * @author audaque * */@XmlRootElement (name= "ADDRESSVO") @ Xmlaccessortype (Xmlaccesstype.field) @XmlType (proporder = {"id", "Address"}) public class address implements serializable { private static final long serialversionuid = -1704617795954765535l; @XmlElement (name= "id") private long id ; @XmlElement (name= "Address") private string address; public address () { } public address (long id, string Address) { this.id = id; this.address = address; } public long getid () { return id; } public void setid ( Long id) { this.id = id; } public string getaddress () { return address; } public void setaddress (String address) { this.address = address; } }
Web. XML
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "/http Java.sun.com/xml/ns/j2ee " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <!-- Specify the location of the spring configuration file by context parameters --> <context-param> < param-name>contextconfiglocation</param-name> <param-value>classpath:spring/ beans.xml</param-value> </context-param> <!-- The spring context Loader listener ensures that the initial   of the spring container is completed when the Web server starts, and is placed in the application range--> <listener> < listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </ listener> <!-- cxf --> <servlet> < Servlet-name>cxfservlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</ servlet-class> <load-on-startup>2</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>cxfservlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping></web-app >
Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/maven-v4_0_0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>cn.sniper.ws</groupId> <artifactId>sniperWS</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>sniperws maven webapp</name> <url>http://maven.apache.org </url> <properties> <project.build.sourceencoding >UTF-8</project.build.sourceEncoding> <spring.version>3.1.1.RELEASE</spring.version> <struts.version>2.3.4.1</struts.version> CXF releases WS services with spring, including soap services, RESTful services