使用CXF实现简单的webservice

添加maven依赖


    org.apache.cxf
    cxf-rt-frontend-jaxws
    3.0.7


    org.apache.cxf
    cxf-rt-transports-http
    3.0.7


    org.apache.cxf
    cxf-rt-transports-http-jetty
    3.0.7

webservice服务端


定义接口 HelloWorld
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}
实现接口 HelloWorldImpl
import me.shenzhun.webservice.service.HelloWorld;
import javax.jws.WebService;

@WebService(endpointInterface = "me.shenzhun.webservice.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello, " + text;
    }
}
配置服务 cxf-servlet.xml

    
    
    

WEB服务配置 web.xml



    Archetype Created Web Application
    
        Apache CXF Endpoint
        cxf
        cxf
        org.apache.cxf.transport.servlet.CXFServlet
        1
    
    
        cxf
        /*
    

    
        contextConfigLocation
        classpath:config/cxf-servlet.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

webservice客户端


配置要调用的服务 beans.xml



    

客户端调用 App
public class App {
    public static void main(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("classpath:config/beans.xml");
        HelloWorld client = (HelloWorld) context.getBean("helloClient");
        System.out.println(client.sayHi("Tom"));
    }
}

你可能感兴趣的:(使用CXF实现简单的webservice)