本文是在SpringMVC基础上添加 CXF的webservice.
CXF官网下载地址 http://cxf.apache.org/download.html
需要添加的最少话jar包为:
cxf-2.7.7.jar
neethi-3.0.2.jar
stax2-api-3.1.1.jar
woodstox-core-asl-4.2.0.jar
wsdl4j-1.6.3.jar
xmlschema-core-2.0.3.jar
我的项目采用的是springMVC并且用注解配置自动装配的方式,
由于本项目的spring配置文件分2部分,一部分为spring的基本配置,如bean的映射等,另一部分为MVC中的servlet常用配置.
Spring配置文件添加
spring-webservice.xml
<?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:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:context="http://www.springframework.org/schema/context"
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://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" default-autowire="byName">
<!-- <context:component-scan base-package="com.tds.gisway.webservice.service.*"/> -->
<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" />
<!-- webService相关配置 end -->
</beans>
编写接口
SdwService.java
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface SdwService {
@WebMethod
public String getQueryResult(@WebParam(name = "username") String username,
@WebParam(name = "password") String password,
@WebParam(name = "methodName") String methodName) throws Exception;
}
接口实现类
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import dxx.webservice.service.SdwService;
@WebService(endpointInterface = "dxx.webservice.service.SdwService")
@Service(value = "sdwService")
public class SdwServiceImpl implements SdwService {
@WebMethod
public String getQueryResult(@WebParam(name = "username") String username,
@WebParam(name = "password") String password,
@WebParam(name = "methodName") String methodName) throws Exception {
String str = "登陆失败";
if("test".equals(username)&&"test".equals(password)){
str = "登陆成功";
}
return str;
}
}
servlet配置添加
webservice-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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://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" default-autowire="byName">
<!-- xmlns:xsd="http://www.w3.org/2001/XMLSchema" -->
<!-- 发布webService -->
<jaxws:endpoint id="SdwService" implementor="#sdwService" address="/sdwService"
implementorClass="dxx.webservice.service.impl.SdwServiceImpl" />
</beans>
web.xml需要调整的地方.
spring文件扫描
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--SpringMVC URL拦截入口-->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/servlet/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
<!-- CXF WebService拦截URL入口 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-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>/webservice/*</url-pattern>
</servlet-mapping>
启动项目后访问 项目路径+webservice+发布地址,
如我的项目访问地址:http://localhost:8081/aide/webservice/sdwService?wsdl
xml文件
lib
访问地址结果
接下来的客户端使用就不说了.