美文网首页
使用CXF实现简单的webservice

使用CXF实现简单的webservice

作者: JohnShen | 来源:发表于2016-01-12 13:42 被阅读215次

添加maven依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.7</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.7</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.0.7</version>
</dependency>

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
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.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-servlet.xml"/>
    <jaxws:endpoint
            id="helloWorld"
            implementor="me.shenzhun.webservice.service.impl.HelloWorldImpl"
            address="/HelloWorld"/>
</beans>
WEB服务配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/cxf-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

webservice客户端


配置要调用的服务 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://cxf.apache.org/jaxws 
       http://cxf.apache.org/schemas/jaxws.xsd">

    <jaxws:client
            id="helloClient"
            serviceClass="me.shenzhun.webservice.service.HelloWorld"
            address="http://127.0.0.1:8080/HelloWorld"/>
</beans>
客户端调用 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

      本文链接:https://www.haomeiwen.com/subject/qtibhttx.html