spring 集成 webservice 进过反复测试终于完成了

489 阅读1分钟

依赖jar,版本号3.1.6

<dependency>

    <groupId>org.apache.cxf</groupId>
    
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    
    <version>3.1.6</version>
    
</dependency>

<dependency>

    <groupId>org.apache.cxf</groupId>
    
    <artifactId>cxf-rt-transports-http</artifactId>
    
    <version>${cxf.version}</version>

</dependency>

<dependency>

    <groupId>org.apache.cxf</groupId>
    
    <artifactId>cxf-rt-bindings-soap</artifactId>
    
    <version>${cxf.version}</version>

</dependency>

编写webservice 接口

import javax.jws.WebService;

@WebService

public interface IDemoWebService {

public String sayHello(String text);

}

编写webservice实现类

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.xtt.platform.util.config.SpringUtil;

import com.xtt.txgl.patient.service.IMedicalOrderService;

@WebService(endpointInterface = "com.xtt.txgl.ws.IDemoWebService")

public class DemoWebServiceImpl implements IDemoWebService {

@Autowired

private IMedicalOrderService orderService;

public String sayHello(String text) {

    System.out.println(SpringUtil.getBean("patientAccountServiceImpl"));
    
    System.out.println(orderService);
    
    return "Hello : " + text;

}

}


配置spring-webservice.xml

<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:jaxws="http://cxf.apache.org/jaxws"

xmlns:cxf="http://cxf.apache.org/core"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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-extension-soap.xml" />

<import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />

<!--下面的class属性值一定要跟你项目中服务实现类的包路径完全一致-->

<bean id="hello" class="com.xtt.txgl.ws.DemoWebServiceImpl"/>

<jaxws:endpoint id="demoWebservice" implementor="#hello" address="/apiws" />

配置web.xml

<param-name>contextConfigLocation</param-name>

<param-value>

classpath*:config/springContext.xml

classpath*:config/webservice.xml//引入webservice配置

</param-value>

处理webservice请求

<servlet>

    <servlet-name>ws</servlet-name>
    
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    
    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>ws</servlet-name>
    
    <url-pattern>/webservice/*</url-pattern>

</servlet-mapping>

访问方式:

http://localhost/cheetah/webservice/apiws?wsdl