最近在使用cxf。在官網上下載demo進行運行,但是老是報錯說什麼Endpoint address不存在等異常,後來發現是因爲pom文件裏面的包沒有引用完整。這裏還是把例子列出來一下,方便進一步理解。
- webservice接口定義(可有可無的)
package com.ws.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @WebService註解讓CXF知道我們希望使用哪個接口來創建WSDL
* mt
*/
@WebService
public interface HelloWorld {
/**
* WebParam 保證xml中的參數名字是正確的 引文被編譯後成.class以後不能保存正確的參數名稱 只有一arg0的樣式來代替
* @param text
* @return
*/
@WebMethod
String sayHi(@WebParam(name="text") String text);
String sayHiToUser(@WebParam(name="user") User user);
void wsMethod();
}
- webservice接口實現類
package com.ws.server;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface="com.ws.server.HelloWorld",serviceName="wsService")
public class HelloWorldImpl implements HelloWorld{
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size() + 1, user);
System.out.println("Hello " + user.getName());
return "Hello " + user.getName();
}
public void wsMethod() {
System.out.println("wsMethod called!");
}
}
- 服務類並且暴露接口
package com.ws.server;
import javax.xml.ws.Endpoint;
import com.ws.server.HelloWorldImpl;
public class Server {
public Server(){
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8090/wsdev/helloWorld";
Endpoint.publish(address, implementor);
}
public static void main(String[] args) throws Exception{
new Server();
System.out.println("Server ready ...");
Thread.sleep(5*60*1000);
System.out.println("Server exiting");
}
}
- 客戶端類 調用服務類接口
package com.ws.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import com.ws.server.HelloWorld;
import com.ws.server.User;
public class WsClient {
private static final QName SERVICE_NAME = new QName("http://server.ws.com/", "HelloWorld");
private static final QName PORT_NAME = new QName("http://server.ws.com/", "HelloWorldPort");
public static void main(String[] args) {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:8090/wsdev/helloWorld";
// If web service deployed on Tomcat deployment, endpoint should be changed to:
// http://localhost:8080/java_first_jaxws-<cxf-version>/services/hello_world
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
String response = hw.sayHi("World");
System.out.println(response);
User user = new User();
user.setName("World");
String userRes = hw.sayHiToUser(user);
System.out.println("userRes:" + userRes);
//say hi to some more users to fill up the map a bit
User user1 = new User();
user.setName("Galaxy");
String sayHiRes = hw.sayHiToUser(user1);
System.out.println("sayHiRes:" + sayHiRes);
}
}
- 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<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" />
<jaxws:server id="jaxwsService" serviceClass="com.ws.server.HelloWorld"
address="/hello_world">
<jaxws:serviceBean>
<bean class="com.ws.server.HelloWorldImpl"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>