首先搭建webservice服务端。
引入CXF的依赖,使用最新的3.5.3版本
<dependencies>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-coreartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-http-jettyartifactId>
<version>3.5.3version>
dependency>
dependencies>
编写TestService接口,并且加上@WebService表示这是一个服务。
package test.service;
import javax.jws.WebService;
@WebService
public interface TestService {
/**
* 测试webservice
* @param test 测试参数
* @return 测试返回值
*/
String test(String test);
}
创建TestService的实现类,并且重写具体的服务类方法。
package test.service.impl;
import test.service.TestService;
import javax.jws.WebService;
@WebService
public class TestServiceImpl implements TestService {
@Override
public String test(String test) {
return test;
}
}
编写服务主类,绑定服务挂载的地址。运行main方法开启webservice服务。
package test;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import test.service.TestService;
import test.service.impl.TestServiceImpl;
public class ServerApplication {
public static void main(String[] args) {
String address = "http://127.0.0.1:8080/testService?wsdl";
TestService testService = new TestServiceImpl();
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
jaxWsServerFactoryBean.setAddress(address);
jaxWsServerFactoryBean.setServiceBean(TestService.class);
jaxWsServerFactoryBean.setServiceBean(testService);
jaxWsServerFactoryBean.create();
}
}
采用springboot框架来模拟webservice客户端。首先导入CXF以来,然后导入springboot 2.0.2.RELEASE的依赖。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.0.2.RELEASEversion>
<scope>importscope>
<type>pomtype>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-coreartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-http-jettyartifactId>
<version>3.5.3version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
首先创建一个controller类,编写一个接口,表示可以从浏览器来请求该接口然后通过该接口访问webservice的服务。
package test.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.client.TestServiceClient;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource
private TestServiceClient testServiceClient;
@RequestMapping("test/{test}")
public String test(@PathVariable String test) throws Exception {
String address = "http://127.0.0.1:8080/testService?wsdl";
String methodName = "test";
return testServiceClient.callTestService(address,methodName,test);
}
}
编写具体的请求webservice的类,operationName表示需求请求webservice具体的服务方法名。params表示该webservice服务方法所需要的参数。
package test.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
@Component
public class TestServiceClient {
/**
* 动态调用webservice接口
* @param wsdUrl 服务地址
* @param operationName 服务名
* @param params 参数
* @return 返回数据
* @throws Exception 异常
*/
public String callTestService(String wsdUrl, String operationName, String... params) throws Exception {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsdUrl);
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects;
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke(method, (Object[]) params);
return objects[0].toString();
}
}
开启服务端和客户端后,请求http://127.0.0.1/test/字符串参数
地址就可以接收到返回值。
No operation was found with the name xxx…
该问题表示webservice服务端的接口和接口的实现类不在同一个包下面,需要在@WebService中添加targetNamespace值。也就是
@WebService(endpointInterface = "test.service.TestService",
targetNamespace = "http://service.test/")
这里的targetNamespace值就是实现类接口的包名倒序。
webservice客户端
https://blog.csdn.net/qq_41687670/article/details/126285098
https://blog.csdn.net/qq_27727251/article/details/101549548
springboot整合webservice
https://blog.csdn.net/weixin_43935907/article/details/103001812
https://blog.csdn.net/weixin_56995925/article/details/120416554
https://blog.csdn.net/sujin_/article/details/83865124
No operation was found with the name错误
https://www.cnblogs.com/xq357100870/p/4053776.html