不說閒話,直接附上所有源碼。
1.pom文件引入jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
2.項目目錄結構
3.接口ISayHello
package com.example.cxfdemo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* @Description TODO
* @Author wuqingyan
* Date 2019/5/17 10:19
* Modify Log
**/
@WebService(targetNamespace = "http://service.cxfdemo.example.com")
public interface IHello {
@WebMethod
public @WebResult String sayHello(@WebParam(name = "userName") String userName);
}
4.實現類SayHelloImpl
package com.example.cxfdemo.serviceimpl;
import com.example.cxfdemo.service.IHello;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
* @Description TODO
* @Author wuqingyan
* Date 2019/5/17 10:22
* Modify Log
**/
@Component
@WebService(serviceName="helloService", //【對外發布的服務名 】:需要見名知意
targetNamespace="http://service.cxfdemo.example.com", //【名稱空間】:要跟接口的保持一致
endpointInterface = "com.example.cxfdemo.service.IHello") //【服務接口全路徑】
public class HelloImpl implements IHello {
@Override
public String sayHello(String userName) {
System.out.println("hello!"+userName);
return "hello!"+userName;
}
}
5.配置類CxfConfig
package com.example.cxfdemo;
import com.example.cxfdemo.serviceimpl.HelloImpl;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @Description TODO
* @Author wuqingyan
* Date 2019/5/17 10:29
* Modify Log
**/
@Configuration
public class CxfConfig {
@Autowired
private HelloImpl hello;
//注意這個方法名 網上好多用dispatcherServlet啓動會報錯
@Bean
public ServletRegistrationBean getDispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");// 發佈服務名稱 localhost:8080/cxf
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint hello() {
// 綁定要發佈的服務實現類
EndpointImpl endpoint = new EndpointImpl(springBus(),hello);
endpoint.publish("/hello"); // 接口訪問地址
return endpoint;
}
}
6.啓動成功截圖
7.客戶端調用類CxfClient
package com.example.cxfdemo.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
* @Description TODO
* @Author wuqingyan
* Date 2019/5/17 11:09
* Modify Log
**/
public class CxfClient {
public static void main(String[] args) {
// 創建動態客戶端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8090/cxf/hello?wsdl");
// 需要密碼的情況需要加上用戶名和密碼
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",參數1,參數2,參數3....);
objects = client.invoke("sayHello", "周杰倫");
System.out.println("返回數據:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
8.調用成功截圖
以上是所有步驟和源碼,歡迎參考。