CXF是目前用的比较多的WebService的实现方式,自己也写了CXF的例子,记录一下。
一.CXF单独使用
服务端
1.创建java工程,导入CXF的jar包,这里用的是3.1.17版本
2.接口类:
import javax.jws.WebService;
@WebService
public interface LanguageService {
public String getLanguage(int position);
}
3.实现类:
public class LanguageServiceImpl implements LanguageService {
@Override
public String getLanguage(int position) {
return "java";
}
}
4.发布服务程序:
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.cxf.service.LanguageService;
import com.cxf.service.LanguageServiceImpl;
public class ServicePublish {
public static void main(String[] args) {
//创建工厂
JaxWsServerFactoryBean sfb = new JaxWsServerFactoryBean();
//设置服务
String address = "http://localhost:8787/Service/cxfService";
sfb.setAddress(address);
sfb.setServiceClass(LanguageService.class);
LanguageService ls = new LanguageServiceImpl();
sfb.setServiceBean(ls);
sfb.getInInterceptors().add(new LoggingInInterceptor());
sfb.getInInterceptors().add(new LoggingOutInterceptor());
//发布服务
sfb.create();
System.out.println("发布成功");
}
}
运行,发布后,打开浏览器,输入:http://localhost:8787/Service/cxfService?wsdl,可以看到生成的wsdl文档。
客户端:
1.新建java工程,导入CXF的jar包
2.打开cmd窗口,执行CXF自带的工具命令wsdl2java,执行如下命令,会生成对应的WebService文件。
wsdl2java -d E:\code\java\webservice\CxfClient1\src -p com.cxf.client http://localhost:8787/Service/cxfService?wsdl
关于wsdl2java命令与jdk自带的wsimport命令的比较可以参看:https://www.cnblogs.com/ChrisMurphy/p/5224160.html
3.编写测试类:
public class ServiceTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(LanguageService.class);
factory.setAddress("http://localhost:8787/Service/cxfService?wsdl");
LanguageService ls = (LanguageService)factory.create();
String result = ls.getLanguage(1);
System.out.println(result);
}
}
执行,可以看到输出:
java
二.CXF与Spring集成使用
这里主要记录服务端的创建方式,客户端的调用方式可以采用spring注入的方式,也可以直接采用上述的客户端调用方式。
1.接口类:
package com.test.service.inter;
import javax.jws.WebService;
@WebService
public interface HelloService {
public String sayHello(String name);
}
2.实现类:
package com.test.service.impl;
import com.test.service.inter.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "您好:"+name;
}
}
3.spring集成CXF的配置文件:
spring的配置文件spring.xml导入CXF配置:
<?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:context="http://www.springframework.org/schema/context"
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">
<import resource="spring-cxf.xml" />
</beans>
CXF的配置文件spring-cxf.xml,两个文件都位于/WEB-INF/目录下面。
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="helloService" implementor="com.cxf.service.impl.HelloServiceImpl" address="/hello">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
其中配置拦截器的作用是打印日志。
4.web.xml文件中配置spring监听器和cxf。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springCxfWebservice</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
5.将项目部署到tomcat容器,启动tomcat服务,访问:http://localhost:8080/CxfSpringService/services,其中"CxfSpringService"为项目的访问名称。可以看到如下界面:
点击wsdl链接,可以到生成的wsdl文件,说明部署成功。
客户端可以沿用第一节中的客户端调用方式。
参考:
http://www.cnblogs.com/jiyukai/p/9249103.html