天天看點

基于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