第一步:創建一個springboot項目並啓動 https://mp.csdn.net/mdeditor/89018104#
第二步:引入jar包
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.7</version>
</dependency>
<!-- CXF webservice -->
第三步:書寫接口類和實現類以及發佈配置
如圖所示:
接口 CommonService
package wang.test.web;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "CommonService", // 暴露服務名稱
targetNamespace = "http://webservice.leftso.com/"// 命名空間,一般是接口的包名倒序
)
public interface CommonService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public String sayHello(@WebParam(name = "userName") String name);
}
實現類CommonServiceImp
package wang.test.web;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
/**
* 接口實現
*
* @author leftso
*
*/
@WebService(serviceName = "CommonService", // 與接口中指定的name一致
targetNamespace = "http://web.test.wang/", // 與接口中的命名空間一致,一般是接口的包名倒
endpointInterface = "wang.test.web.CommonService"// 接口地址
)
@Component
public class CommonServiceImp implements CommonService {
@Override
public String sayHello(String name) {
return "Hello ," + name;
}
}
發佈配置CxfConfig
package wang.test.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import wang.test.web.CommonService;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
CommonService commonService;
/** JAX-WS **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/CommonService");
return endpoint;
}
}
第四步:啓動服務並訪問生成xml 出現下圖即爲成功
問題1 Cxf開發webservice時遇到Can’t find the request for xx’s Observer
我這邊的錯誤是springboot版本不兼容的問題 我之前用的是1.5.4.RELEASE 後改爲1.4.5.RELEASE
就可以了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
: ) 補充-----------------------------------------------------------------------------------------------------------
客戶端
步驟一:在另一個項目中 右鍵—新建—Web Service Client—下一步
步驟二:將服務端發佈的wsdl路徑添加到Service definition 然後完成
生成文件如下:
步驟三:新建一個類
import java.rmi.RemoteException;
public class EditSrceeningTest {
public static void main(String[] args) {
/* MobileCodeWS mc = new MobileCodeWS();
MobileCodeWSSoap soap = mc.getMobileCodeWSSoap();
String str = soap.getMobileCodeInfo("13011286707", null);
System.out.println(str);*/
/**
* 利用axis調用webservice實例
* @param args
*/
try {
//換成對應的proxy類
EditScreeningProxy proxy = new EditScreeningProxy();
proxy.setEndpoint("http://10.11.23.76:8803/services/EditScreening?wsdl");
//換成獲取對應的serice
EditScreening_PortType service = proxy.getEditScreening_PortType();
//調用web service提供的方法
String result = service.edit("客戶端傳輸---");
System.out.println(result);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
如圖:
啓動main方法!獲得返回數據成功!(備註: 我這裏還有點問題 -_-)