标签:
CXF结合spring发布WS服务,含SOAP services、RESTful services
1、访问:http://localhost:8088/sniperWS/services/
查看有哪些服务,包含Available SOAP services、Available RESTful services
2、客户端调用RESTful services:
http://localhost:8088/sniperWS/services/address/getSuggestions.query
调用示例:
$.ajax({
url: "http://ip:port/sniperWS/services/address/getSuggestions.query",
dataType: "json",
data : {
"address" : $("#address").val(),
"max":10,
"_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发布web服务配置 --> <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" /> <!-- 方式一:发布SOAP services --> <!-- <jaxws:endpoint id="addressWS" implementor="#addressService" address="/addressWS" /> --> <!-- 方式二:发布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> </jaxws:inInterceptors> --> </jaxws:server> <!-- 发布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-aop-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; /** * 注意,@XmlRootElement(name="addressVO")的name不要和属性的name重复, * 否则会导xml解析过程报错 * @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"> <!-- 通过上下文参数指定spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/beans.xml</param-value> </context-param> <!-- spring上下文载入器监听器,确保web服务器启动时,完成spring容器的初始化 ,放在了application范围中--> <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> <hibernate.version>3.6.9.Final</hibernate.version> <junit.version>4.8.1</junit.version> </properties> <dependencies> <!-- spring配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- 注解拦截,需要引入spring aop相关jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- spring jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-hibernate3</artifactId> <version>2.0.8</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <!-- aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjlib</artifactId> <version>1.6.2</version> </dependency> <!-- @ResponseBody 将数据转换成json的依赖包,如果没有依赖包,即使能够访问action,返回的时候也会报错 --> <!-- 解决:Could not find acceptable representation报错; 客户端报错:ajax not acceptable --> <!-- <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> --> <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>2.6.1</version> <type>pom</type> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.2.3</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>sniperWS</finalName> </build> </project>
CXF结合spring发布WS服务,含SOAP services、RESTful services
标签:
原文地址:http://my.oschina.net/sniperLi/blog/494391