1.Spring+CXF 全注解版 搭建 Spring3.1.1 CXF 2.5.2
2.部分配置
1)web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="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"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <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>
2)applicationContext-cxf.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<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" />
<context:component-scan base-package="com.j4t.demo.ws" />
<bean id="webServicesAgent" class="org.apache.cxf.spring.remoting.Jsr181HandlerMapping">
<property name="urlPrefix"><value>/</value></property>
</bean>
</beans>
3)java代码
package com.j4t.demo.ws;
import javax.jws.WebService;
//接口
@WebService
public interface IHelloService {
public String sayHello(String username);
}
package com.j4t.demo.ws;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
//实现
@Component
@WebService(serviceName = "helloService", endpointInterface = "com.j4t.demo.ws.IHelloService")
public class HelloService implements IHelloService {
public String sayHello(String username) {
return username+"! Welcome to CXF in Method[HelloService sayHello]";
}
}
3)客户端
服务地址:http://192.168.1.100:8080/Demo_Spring_CXF_Annotation/ws
client调用代码
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient("http://192.168.1.100:8080/Demo_Spring_CXF_Annotation/ws/helloService?wsdl");
Object[] reply = client.invoke("sayHello", new Object[] { "just4it" });
System.out.println(reply[0]);