如何来用cxf结合spring开发webservice接口。by@wangkun
package com.webservice.server.wangkun;
/**
* Created by wangkun on 2015/9/21.
*/
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
package com.webservice.server.wangkun;
/**
* Created by wangkun on 2015/9/21.
*/
import javax.jws.WebService;
@WebService(endpointInterface = "com.webservice.server.wangkun.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
CXF包含在Spring的“好XML”的支持。对于JAX-WS,建一个 bean服务器端的endpoint。
在WEB-INF目录下的CXF-servlet.xml文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="helloWorld" implementor="com.webservice.server.wangkun.HelloWorldImpl" address="/HelloWorld"/>
</beans>
如果你想弹性的引用bean可以这样写:
<bean id="hello" class="demo.spring.service.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>/webService/*</url-pattern>
</servlet-mapping>
</web-app>
打开cmd,cd到wsdl文档所在的目录(该wsdl文档是服务端所产生的),执行命令
wsdl2java wsdl2java F:\webservice_project\cxfClient\src\HelloWorld.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--一种方式-->
<bean id="helloClient" class="com.webservice.server.wangkun.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.webservice.server.wangkun.HelloWorld"/>
<property name="address" value="http://localhost:8080/webService/HelloWorld"/>
</bean>
<!--另外一种jaxws:client方式 -->
<!-- <jaxws:client id="helloClient"
serviceClass="com.webservice.server.wangkun.HelloWorld"
address="http://localhost:8080/webService/HelloWorld" />-->
</beans>
package com.webservice.test;
import com.webservice.server.wangkun.HelloWorld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
String response = client.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}
如果需要源码请加qq:398121505 或者给我留言,谢谢!