最近在使用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>