最近写接口,之后就对接口的简单实现做个小结:
可以在传统的JAVA EE的基础上增加一层Web Service层,这样我们的JAVA EE就可以对外暴露Web Service,这样任何平台、任何语言就可以通过程序来调用此接口了!
具体实现步骤:
1、添加CXF的相关jar包和spring的相关jar包(具体jar包根据自己的实际需求情况而定);
commons-logging.jar
cxf.jar
neethi.jar
wsdl4j.jar
xmlschema-core.jar
spring.jar
spring-asm.jar
spring-expression.jar
2、在WEB.xml中配置CXF的核心控制器,如:
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.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>/cxf/*</url-pattern>
</servlet-mapping>
</web-app>
3、在spring的配置文件中导入CXF提供的Schema、XML配置文件;在spring的配置文件中使用axws:endpoint元素来暴露WEB Service。具体如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.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"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<jaxws:endpoint id="cxfSpringService" address="/cxfSpringService" implementorClass="com.cxf.spring.service.CXFSpringService">
<jaxws:implementor>
<bean id="cXFSpringServiceImpl" class="com.cxf.spring.service.impl.CXFSpringServiceImpl"/>
</jaxws:implementor>
</jaxws:endpoint>
</beans>
4、书写简单的WEB Service层:
接口:CXFSpringService
package com.cxf.spring.service;
import javax.jws.WebService;
@WebService
public interface CXFSpringService {
public String cxfTest1();
public void cxfTest2();
}
接口实现:CXFSpringServiceImpl
package com.cxf.spring.service.impl;
import javax.jws.WebService;
import com.cxf.spring.service.CXFSpringService;
//endpointInterface定义实现的接口,serviceName定义发布的服务名
@WebService(endpointInterface="com.cxf.spring.service.CXFSpringService",serviceName="CXFSpring")
public class CXFSpringServiceImpl implements CXFSpringService {
public String cxfTest1() {
return "CXFSpringServiceImpl";
}
public void cxfTest2() {
System.out.println("CXFSpringServiceImpl");
}
}
5、部署该WEB应用,启动tomcat,在浏览器中访问地址:http://localhost:8080/WS_cxf_Spring/cxf/cxfSpringService?wsdl。如正常出现相关的xml文件,就证明该接口正常启动:
<?xml version="1.0"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.spring.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" targetNamespace="http://service.spring.cxf.com/"
name="CXFSpringServiceService">
到此,一个简单的WEB接口就成功编写和启动成功,就可以编写客户端来访问该接口啦!