本文是在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
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">
编写接口
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
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">
web.xml需要调整的地方.
spring文件扫描
classpath*:/spring/*.xml
org.springframework.web.context.ContextLoaderListener
classpath*:/servlet/*.xml
org.apache.cxf.transport.servlet.CXFServlet
启动项目后访问 项目路径+webservice+发布地址,
如我的项目访问地址:http://localhost:8081/aide/webservice/sdwService?wsdl
xml文件
lib
接下来的客户端使用就不说了.