Web Service (四) 手动发布Web Service接口-CXF与Spring集成

       当我们发布完Web Service接口之后有两种方式可以调用Web service服务,一种是通过动态客户端方式,另一种是引用服务端的接口,引用服务端接口的方式对于客户端同服务器端耦合比较大,而使用WSDL的方式客户端不知道服务端的存在就可以调用服务器的方法。

       下面是项目的结构图:

       1.Web Service发布项目



2.编写服务端接口类以及实现类,如下,同上一篇自动发布接口,多了一个注解@WebService

package com.test.webservice;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(User user);
	public String sayHello1();
}

package com.test.webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(endpointInterface="com.test.webservice.IHelloWorld",serviceName="helloService123")
public class HelloWorldImpl implements IHelloWorld {

	@Override
	public String sayHello(User user) {
		System.out.println("开始调用Web Service method:sayHello()");
		return user.getUsername()+"lilongsheng";
	}

	@Override
	@WebMethod
	public String sayHello1() {
		System.out.println("开始调用Web Service method:sayHello1()");
		return "lilongsheng1";
	}

}
       在实现类上面还可以加上SOAP注解,如

@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
       表示Web Service方法传递数据的类型、编码等设置。

参考:http://dlc-cdn.sun.com/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh_CN/api/javax/jws/soap/SOAPBinding.html

3.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: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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="helloService456" implementor="com.test.webservice.HelloWorldImpl" address="/helloService789"/>
	
	
</beans>

4.客户端测试代码

package com.test.webservice;

//import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class testClient {
	public static void main(String[] args) {  
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
        //创建客户端
        Client client = dcf.createClient("http://192.168.24.82:8080/Web_Service_Spring/ws/helloService789?wsdl");  
        Object[] objects;  
        try {  
        	User user=new User();
        	user.setUsername("longsheng");
            objects = client.invoke("sayHello",user);  
            //输出调用结果  
            System.out.println(objects[0].toString());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
    }  
}

       上面传参为传实体,实体需要实现序列化接口才能先通过网络传输。Web service在系统间以及系统交互上用的很广泛。


转载于:https://www.cnblogs.com/lilongsheng1125/p/4978502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值