Spring 使用 WebService(CXF)

本文介绍如何通过Eclipse的WebServiceClient工具及JAX-WS技术来实现Web服务的客户端调用。包括配置依赖、生成客户端代码、配置Spring项目等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用web service client

1.打开eclipse点击new选择Web Service Client。


2.输入url:如http://127.0.0.1/SSM/webservice/SayHelloService?wsdl

3.如果url合法则可以点击下一步,选择代码生成后保存到那个Spring项目下。


4.点击finish,如果生成的代码有错,原因是少个axis.jar包。

<dependency>
		    <groupId>org.apache.axis</groupId>
		    <artifactId>axis</artifactId>
		    <version>1.4</version>
		</dependency>
5.调用代理

@GetMapping("/sayHello")
	public @ResponseBody String sayHello(String name) throws Exception {
		name = new String(name.getBytes("iso-8859-1"),"utf-8");
		SayHelloServiceProxy proxy = new SayHelloServiceProxy();
		return proxy.sayHello(name);
	}

二、使用jaxws:client

1.引入jar包

<!--cxf-->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.6</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
2.配置web.xml

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-cxf.xml</param-value>
	</context-param>
3.导入接口

package com.it.service;

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

@WebService
public interface SayHelloService {
	
	@WebMethod
	public String SayHello(String name);
}
4.配置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: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">
  <!-- 引cxf的一些核心配置 -->
  <import resource="classpath:META-INF/cxf/cxf.xml"/>
  
  <jaxws:client id="SayHelloServiceClient"
  				serviceClass="com.it.service.SayHelloService"
  				address="http://127.0.0.1/SSM/webservice/SayHelloService"				
  ></jaxws:client>
</beans>

id:可以随便写但是不能重复。

serviceClass:第三步导入的接口。

address:发布接口的地址。ip:port/项目名/webservice/{address},

可以看上篇文章Spring 发布WebService(CXF)   http://blog.csdn.net/qq_33422712/article/details/79205315

5.使用接口调用。

@Autowired
	private SayHelloService sayHelloService;
	
	@GetMapping("/sayHello")
	public @ResponseBody String sayHello(String name) throws Exception {
		name = name !=null?name:"";
		name = new String(name.getBytes("iso-8859-1"),"utf-8");
		return sayHelloService.SayHello(name);
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值