Spring Boot项目WebService接口发布、调用、以及常见错误详解
一、Spring Boot项目发布WebService接口
- 添加maven依赖:
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.5</version></dependency>
- 定义Webservice接口:
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService
public interface JobListService {@WebMethodString getList(@WebParam(name = "userId") String userId, @WebParam(name = "agentNum") Integer agentNum);
}
- 定义接口实现类:targetNamespace 的值最好是包名反写,不是也没关系。endpointInterface 是webservice接口的地址,注意给这个类添加@Component直接注入到spring中
import javax.jws.WebService;import org.springframework.stereotype.Component;@WebService(targetNamespace = "http://webservice.test.fc.com/",
endpointInterface = "com.fc.test.webservice.JobListService")
@Component
public class JobListServiceImpl implements JobListService{@Overridepublic String getList(String userId, Integer agentNum) {return "请求成功";}}
- 定义webservice接口服务的配置类:该类的作用是将改webservice服务以jobListService的名称发布出去。此处一定要注意springboot整合了shiro,需要把webservice路径添加到过滤路径上去
import javax.xml.ws.Endpoint;import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class WebServiceConfig {@Autowiredprivate JobListService jobListService;/*** 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet* @return*/@Bean(name = "cxfServlet")public ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(),"/services/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), jobListService);endpoint.publish("/jobListService");return endpoint;}
}
在ShiroFilterMapFactory中的shiroFilterMap()方法,加上filterChainDefinitionMap.put("/services/**", "anon");此时启动项目就发布成功了
- 启动springboot项目:一定要注意使用jdk运行项目不可用jre,必须要有jdk中的tools.jar;此处注意localhost有时不行,可以用127.0.0.1
访问如下目录:ip+端口/services/服务名称?wsdl
例如:http://localhost:8081/services/jobListService?wsdl
二、SpringBoot项目调用WebService接口,测试了第二种动态调用方式可用,其他两种方式未测试
//import javax.xml.rpc.ParameterMode;
//
//import org.apache.axis.client.Call;
//import org.apache.axis.client.Service;
//import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class TestWebSevice {public static void main(String[] args) {
// TestWebSevice.main1();TestWebSevice.main2();
// doSelectRiskReportForm();}/*** 1.代理类工厂的方式,需要拿到对方的接口地址*/public static void main1() {try {// 接口地址String address = "http://127.0.0.1:8080/services/jobListService?wsdl";// 代理工厂JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();// 设置代理地址jaxWsProxyFactoryBean.setAddress(address);// 设置接口类型jaxWsProxyFactoryBean.setServiceClass(JobListService.class);// 创建一个代理接口实现JobListService us = (JobListService) jaxWsProxyFactoryBean.create();// 数据准备String userId = "zz";// 调用代理接口的方法调用并返回结果String result = us.getList("", 1);System.out.println("返回结果:" + result);} catch (Exception e) {e.printStackTrace();}}/*** 2:动态调用*/public static void main2() {// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://127.0.0.1:8080/services/jobListService?wsdl");// 需要密码的情况需要加上用户名和密码// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects = new Object[0];try {//如果有命名空间需要加上这个,第一个参数为命名空间名称,第二个参数为WebService方法名称QName operationName = new QName("http://webservice.test.fc.com/","getList");// invoke("方法名",参数1,参数2,参数3....);objects = client.invoke(operationName, "maple",2);System.out.println("返回数据:" + objects[0]);} catch (java.lang.Exception e) {e.printStackTrace();}}// public static void doSelectRiskReportForm(){
// //调用接口
// //方法一:直接AXIS调用远程的web service
// try {
// String endpoint = "http://localhost:8080/services/jobListService?wsdl";
// Service service = new Service();
// Call call = (Call) service.createCall();
// call.setTargetEndpointAddress(endpoint);
// String parametersName = "settle_num"; // 参数名//对应的是 public String printWord(@WebParam(name = "settle_num") String settle_num);
// call.setOperationName("getList"); // 调用的方法名//当这种调用不到的时候,可以使用下面的,加入命名空间名call.setOperationName(new QName("http://jjxg_settlement.platform.bocins.com/", "printWord"));// 调用的方法名
// call.addParameter(parametersName, XMLType.XSD_STRING, ParameterMode.IN);//参数名//XSD_STRING:String类型//.IN入参
// call.setReturnType(XMLType.XSD_STRING); // 返回值类型:String
// String message = "123456789";
// String result = (String) call.invoke(new Object[] { message });// 远程调用
// System.out.println("result is " + result);
// } catch (Exception e) {
// System.err.println(e.toString());
// }
// }}
三、接口调试工具SoapUI
免安装版下载链接:https://download.csdn.net/download/wgd930701/12701666
Spring Boot项目WebService接口发布、调用、以及常见错误详解相关推荐
- Spring Boot项目中使用RestTemplate调用https接口出现 unable to find valid certification path to requested target
问题描述:Spring Boot项目中使用RestTemplate调用https接口出现以下错误: PKIX path building failed: sun.security.provider.c ...
- spring boot 项目在启动时调用接口
1.环境 目前开发的项目使用的spring boot(2.1.4.RELEASE)+ssm 2. 需求 现在有一个数据处理任务的接口,在spring boot项目启动后,可以手动的去启动任务,但是这样 ...
- spring boot集成webservice接口
依赖集成 当前spring boot 版本是 2.0.1.RELEASE, 其对应的cxf依赖版本为:3.2.4, 详情如下: <dependency><groupId>org ...
- Spring Boot项目(Maven\Gradle)三种启动方式及后台运行详解
Spring Boot项目三种启动方式及后台运行详解 1 Spring Boot项目三种启动方法 运行Application.java类中的Main方法 项目管理工具启动 Maven项目:mvn sp ...
- 项目启动时socket自动启动_spring boot 项目在启动时调用接口
1.环境 目前开发的项目使用的spring boot(2.1.4.RELEASE)+ssm 2. 需求 现在有一个数据处理任务的接口,在spring boot项目启动后,可以手动的去启动任务,但是这样 ...
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
STS创建Spring Boot项目实战(Rest接口.数据库.用户认证.分布式Token JWT.Redis操作.日志和统一异常处理) 1.项目创建 1.新建工程 2.选择打包方式,这边可以选择为打 ...
- idea springboot 发布webservice 发布服务_太赞了:Spring boot+redis实现消息发布与订阅...
一.创建spring boot项目 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot s ...
- Spring Boot + Dataway :接口不用写,配配就出来?
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 哈库纳 来源 | my.oschina.net ...
- 基于Spring Boot应用Apache CXF发布Web Services服务
记录:298 场景:使用Spring Boot应用Apache CXF发布Web Services服务,实现跨系统之间交互接口. 版本: JDK 1.8 Spring Boot 2.6.3 Apach ...
- Vue + Spring Boot 项目实战(十七):后台角色、权限与菜单分配
重要链接: 「系列文章目录」 「项目源码(GitHub)」 本篇目录 前言 一.角色.权限分配 1.用户信息表与行数据获取 2.角色分配 3.权限分配 二.菜单分配 下一步 前言 有感于公司旁边的兰州 ...
最新文章
- 深度学习中的图像分割:方法和应用
- Restful与webService区别
- spring beans源码解读之--BeanFactory的注册
- 红队攻防之从边界突破到漫游内网(无cs和msf)
- 科大星云诗社动态20210811
- iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls...
- Java牛客专项练习2020.12.10
- Java并发编程之volatile关键字
- linux进程自动启动,linux 嵌入式 自启动 系统自动登录-自动启动程序或脚本
- hwclock(Linux)
- 利用扩展双屏技术及Chrome浏览器,快速剖析优秀网页Div及CSS构成,并高效实现原型创作
- Ansible之playbook的使用总结 - 运维笔记
- js图片上传(配合七牛云)
- echarts的示例二:饼图(南丁格尔图)
- 学习如逆水行舟,不进则退
- 最新主流大数据技术分类大全(持续更新)
- epoll检测对端关闭
- 线性代数基础知识:求矩阵的特征值、特征向量和协方差矩阵
- 如何让 Eclipse Java EE 版安装 CDT 以同时支持 Java 和 C/C++ 开发
- Ubuntu服务器的安装和配置----系统安装