spring项目创建webservice服务,以及接口验证方式

spring项目创建webservice服务,以及接口验证方式

依赖jar包cxf

下载方式 http://cxf.apache.org/download.html
需要最少的jar如下:

cxf-2.3.3.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.2.1.1.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
wstx-asl-3.2.9.jar

创建webservice服务器

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 创建一个服务接口;
package com.service;  
  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService  
public interface IHelloWorld {  
    public String sayHello(@WebParam(name = "arg0") String text);  
}  
  1. 接口实现类
package com.service.impl;  
  
import javax.jws.WebService;  
  
import com.service.IHelloWorld;  
  
@WebService(endpointInterface = "com.service.IHelloWorld")  
public class HelloWorldImpl implements IHelloWorld {  
    public String sayHello(String text) {  
        return "Hello : " + text;  
    }  
} 
  1. ,创建spring配置文件,将服务类加入到容器中
<?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" />  
    <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  
  
    <!--下面的class属性值一定要跟你项目中服务实现类的包路径完全一致-->  
    <bean id="hello" class="com.service.impl.HelloWorldImpl"/>  
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />  
</beans> 
  1. 在web.xml中添加webservice.xml配置文件;
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
        /WEB-INF/webservice.xml       
    </param-value>  
</context-param>  
  1. 在web.xml中加入cxf servle
<servlet>  
    <display-name>CXF Servlet</display-name>  
    <servlet-name>CXFServlet</servlet-name>  
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
  
<servlet-mapping>  
    <servlet-name>CXFServlet</servlet-name>  
    <url-pattern>/webservice/*</url-pattern>  
</servlet-mapping>

创建webservice客户端

  1. 首先要创建一个和服务器端一样的服务接口,(如果客户端和服务器端在同一个项目中则可以省略这步)
package com.service;  
  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService  
public interface IHelloWorld {  
    public String sayHello(@WebParam(name = "arg0") String text);  
}  
  1. 创建spring-client.xml,
<?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/schema/jaxws.xsd">  
  
    <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />  
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
        <property name="serviceClass" value="com.service.IHelloWorld" />  
        <property name="address" value="http://localhost:8080/test/HelloWorld" />  
    </bean>  
</beans>  
  1. 测试类
package com.test;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
import com.service.IHelloWorld;  
  
public class Test {  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");  
        IHelloWorld client = (IHelloWorld) ctx.getBean("client");  
        String result = client.sayHello("你好!");  
        System.out.println(result);  
    }  
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值