WebService整合spring

来源:互联网 发布:保温隔热材料知乎 编辑:程序博客网 时间:2024/05/18 00:27

前言

关于webservice的理论不再叙述了,见我前两篇博客,这里以一个简单的例子,叙述cxf和spring的整合。

服务端

  1. webservice对外接口
    webservice作为服务对外发布的时候,暴露的都是一个接口,这里一个HelloWorld为例,这个接口中只包含一个sayHello方法。
import javax.jws.WebService;@WebServicepublic interface HelloWorld {    public String  sayHello(String name);}
  1. webservice接口实现
    接口实现类也很简单,只是将输入的name,进行简单输出。
import javax.jws.WebService;@WebServicepublic class HelloWorldImpl implements HelloWorld {    public String  sayHello(String name) {        return "Hello World"+name;    }}
  1. 发布
    编写好服务端代码后,就可以将其作为服务发布,这里我们选择结合spring进行。利用jaxws:endpoint元素发布服务。
<?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:p="http://www.springframework.org/schema/p"      xmlns:jaxws="http://cxf.apache.org/jaxws"      xmlns:cxf="http://cxf.apache.org/core"      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       http://cxf.apache.org/jaxws       http://cxf.apache.org/schemas/jaxws.xsd">      <import resource="classpath:META-INF/cxf/cxf.xml" />      <jaxws:endpoint id="helloWorld" implementor="com.sf.webservice.HelloWorldImpl" address="/HelloWorld"/></beans><!-- END SNIPPET: beans -->

这里implementor是指服务的实现类,address是服务的地址,客户端后面要通过这个地址访问。

客户端

客户端我们同样结合spring进行,利用jaxws:client元素声明。

<?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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">              <jaxws:client id="client" address="http://localhost:8081/webservice/HelloWorld"        serviceClass="com.sf.webservice.HelloWorld" /></beans>

这里address是服务发布的地址,其中webservice是我的项目名称。serviceClass是服务端的对外服务接口。
这里我们客户端用个main函数来测试我们的项目是否成功。

package com.sf.webservice;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldClient {        public static void main(String args[]) throws Exception {             ApplicationContext ctx = new ClassPathXmlApplicationContext("client-beans.xml");               HelloWorld client = (HelloWorld) ctx.getBean("client");                  String result = client.sayHello("你好!");                  System.out.println(result);          }}

我们的项目是个maven工程,这里给出相应的maven配置。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.sf.webservice</groupId>    <artifactId>webservice</artifactId>    <packaging>war</packaging>    <version>0.0.1-SNAPSHOT</version>    <name>webservice Maven Webapp</name>    <url>http://maven.apache.org</url>    <properties>        <!--spring版本号 -->        <spring.version>4.0.2.RELEASE</spring.version>        <!--指定Maven用什么编码来读取源码及文档 -->        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!--指定Maven用什么编码来呈现站点的HTML文件 -->        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <cxf.version>3.0.4</cxf.version>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>        <!-- CXF -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-databinding-aegis</artifactId>            <version>${cxf.version}</version>        </dependency>        <!-- spring核心包 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-oxm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>xfire</groupId>            <artifactId>jaxws-api</artifactId>            <version>2.0</version>        </dependency>    </dependencies>    <build>        <finalName>webservice</finalName>    </build></project>
0 0
原创粉丝点击