springboot整合webservice采用CXF技術


轉載自:https://blog.csdn.net/qq_31451081/article/details/80783220

強推:https://blog.csdn.net/chjskarl/article/details/52052771

最好:     https://blog.csdn.net/xie19900123/article/details/83986482

第一種:

依賴:

 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
/**
 * WebService接口
 * @作者 Administrator
 * @創建日期 2018/6/23 0023
 * @創建時間 11:21.
 */
@WebService(name = "CommonService", // 暴露服務名稱
        targetNamespace = "http://www.WebService.demo.example.com")   //命名空間,一般是接口的包名倒序
public interface CommonService {
    @WebMethod
    @WebResult(name = "String",targetNamespace = "")
    public String HelloWorld(@WebParam(name = "HelloName") String name);
}
/**接口實現
 * @作者 Administrator
 * @創建日期 2018/6/23 0023
 * @創建時間 11:26.
 */
@WebService(serviceName = "CommonService",//與前面接口一致
        targetNamespace = "http://www.WebService.demo.example.com",  //與前面接口一致
        endpointInterface = "com.webservice.demo.webservice.CommonService")  //接口地址
@Component
public class CommonServiceImpl implements CommonService {
    @Override
    public String HelloWorld(String name) {
        return "Hello World!!! --->"+name;
    }
}
 
public class CxfClient {
    public static void main(String[] args) {
        cl1();
    }
 
    /**
     * 方式1.代理類工廠的方式,需要拿到對方的接口
     */
    public static void cl1() {
        try {
            // 接口地址
            String address = "http://localhost:8080/services/CommonService?wsdl";
            // 代理工廠
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 設置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 設置接口類型
            jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
            // 創建一個代理接口實現
            CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
            // 數據准備
            String userName = "Leftso";
            // 調用代理接口的方法調用並返回結果
            String result = cs.HelloWorld(userName);
            System.out.println("返回結果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@Configuration
public class WebConfig {
    @Autowired
    private Bus bus;

    @Autowired
    CommonService service;
 
    /*jax-ws*/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, service);
        endpoint.publish("/CommonService");
        return endpoint;
    }

第2種:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM