天天看点

基于JAX-WS的WebService在Spring框架中的实现与调用

一、服务端的实现

1、配置web.xml

<!-- 当访问 http://主机名:端口号/项目名/services/.. 时,先进入CXFServlet类-->
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
           

2、配置spring-servlet.xml

<!-- cxf版本低于3.0时,需引入3个xml文件 -->
    <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"/> 
<!-- cxf版本为3.0及以上时,只需引入1个xml文件 -->
    <import resource="classpath:META-INF/cxf/cxf.xml"/> 

<!-- 
address属性:与xml里配置的url-pattern相结合,当访问 http://主机名:端口号/项目名/services/EMRServ 时,进入implementor属性指定的类。其对应的wsdl地址即为 http://主机名:端口号/项目名/services/EMRServ?wsdl
implementor属性:可直接指定实现接口的类名,也能以"#REF_BEAN_NAME"的形式,指定以注解方式声明的类名
-->
    <jaxws:endpoint id="EMRServerBean" implementor="#EMRServer" address="/EMRServ" publish="true" > 
        <jaxws:features> 
             <bean class="org.apache.cxf.feature.LoggingFeature" /> 
       </jaxws:features>  
    </jaxws:endpoint>
           

3、服务端接口 EMRServer.java

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace="http://impl.EMRServer.com")
public interface EMRServer {
  //@WebParam注解作用:在@WebService发布成wsdl时候, 方法的参数名会称被自动的映射成arg0, arg1。通过@WebParam注解可将方法参数名重新设置成自己想要的
    public String patientInfo(@WebParam(name = "json", targetNamespace="http://impl.EMRServer.com")String json);

    public String medicalRecords(@WebParam(name = "json", targetNamespace="http://impl.EMRServer.com")String json);

    public String medicalRecordsDetail(@WebParam(name = "json", targetNamespace="http://impl.EMRServer.com")String json);
}
           

4、服务端接口实现 EMRServerImp.java

import com.cpinfo.his.web.cxf.EMRServer;

//若设置serviceName属性的话,生成的客户端类名为 EMRServer_Service
//若不设置该属性,生成的客户端类名为 EMRServerImpService
@WebService(serviceName="EMRServer",targetNamespace="http://impl.EMRServer.com")

//与spring-servlet.xml文件中的<jaxws:endpoint>配置的implementor属性相对应
@Service("EMRServ") 
public class EMRServerImp implements EMRServer {
  public String patientInfo(String json){
    //TODO
  }

  public String medicalRecords(String json) {
    //TODO
  }

  public String medicalRecordsDetail(String json) {
    //TODO
  }
}
           

二、在服务端编写测试类

//在服务端可直接写测试类
public class TestWTao {
    public static void main(String[] args) {

      //直接实例化实现接口的类,然后调用类的方法
       EMRServerImp eg = new EMRServerImp(); 

       String json = "{ 'visitNumber':'341281_8816' , 'subRecordHtmlID':'341281_4028b0f75722c8cf015722ccb57d002e' ,'hospitalNo':'341281_5662_5662'}";
       String test = eg.medicalRecordsDetail(json); 

    }
           

三、在客户端调用接口

public class EMRTest {

    public static void main(String[] args) {

      //找到客户端生成的类当中,以"Service"结尾的类,找到该类的最后一个方法,该方法名为getEMRServerImpPort,该方法的返回类型为EMRServer
        EMRServer eg = new EMRServer_Service().getEMRServerImpPort();    

        String json = "{ 'visitNumber':'341281_8816' , 'subRecordHtmlID':'341281_4028b0f75722c8cf015722ccb57d002e' ,'hospitalNo':'341281_5662_5662'}";
      //调用接口的方法 
        String test = eg.medicalRecordsDetail(json2); 
    }
}
           

客户端生成的类当中,以“Service”结尾的类

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;

/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-
 * Generated source version: 2.0
 * <p>
 * An example of how this class may be used:
 * 
 * <pre>
 * EMRServer service = new EMRServer();
 * EMRServer portType = service.getEMRServerImpPort();
 * portType.medicalRecordsDetail(...);
 * </pre>
 * 
 * </p>
 * 
 */
@WebServiceClient(name = "EMRServer", targetNamespace = "http://impl.EMRServer.com", wsdlLocation = "http://localhost:8090/webservice1/services/EMRServer?wsdl")
public class EMRServer_Service extends Service {

    private final static URL EMRSERVER_WSDL_LOCATION;
    private final static Logger logger = Logger
            .getLogger(com.wtao.service.EMRServer_Service.class.getName());

    static {
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = com.wtao.service.EMRServer_Service.class.getResource(".");
            url = new URL(baseUrl,
                    "http://localhost:8090/webservice1/services/EMRServer?wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8090/webservice1/services/EMRServer?wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        EMRSERVER_WSDL_LOCATION = url;
    }

    public EMRServer_Service(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public EMRServer_Service() {
        super(EMRSERVER_WSDL_LOCATION, new QName("http://impl.EMRServer.com",
                "EMRServer"));
    }

    /**
     * 
     * @return returns EMRServer
     */
    @WebEndpoint(name = "EMRServerImpPort")
    public EMRServer getEMRServerImpPort() {
        return super.getPort(new QName("http://impl.EMRServer.com",
                "EMRServerImpPort"), EMRServer.class);
    }

}
           

相关连接:

Java WebService 简单实例

spring注解方式,使用jax-ws配置webservice,适合小白

彻底理解webservice SOAP WSDL