一、使用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.youkuaiyun.com/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);
}