springboot 配置webservice接口

导入依赖的jar

<!-- webservice cxf --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.6</version></dependency>

创建webservice配置类

import com.xbsafe.webservice.service.DemoService;
import com.xbsafe.webservice.service.DemoServiceImpl;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configuration
public class CxfConfig {@Beanpublic ServletRegistrationBean dispatcherServlet() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/demo/*");servletRegistrationBean.setName("webService");return servletRegistrationBean;}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic DemoService demoJsonService(){return new DemoServiceImpl();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), demoJsonService());endpoint.publish("/ws");endpoint.getInInterceptors().add(new WsInterceptor()); //add webservice inteceptorreturn endpoint;}
}

创建webservice拦截器类

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.AbstractLoggingInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.DelegatingInputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.slf4j.LoggerFactory;import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.logging.Logger;public class WsInterceptor extends AbstractLoggingInterceptor {private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(WsInterceptor.class);public WsInterceptor() {super(Phase.RECEIVE);}@Overridepublic void handleMessage(Message message) throws Fault {InputStream is = message.getContent(InputStream.class);if(is!=null){CachedOutputStream bos = new CachedOutputStream();if (threshold > 0) {bos.setThreshold(threshold);}try {// use the appropriate input stream and restore it laterInputStream bis = is instanceof DelegatingInputStream? ((DelegatingInputStream)is).getInputStream() : is;//only copy up to the limit since that's all we need to log//we can stream the restIOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);bos.flush();bis = new SequenceInputStream(bos.getInputStream(), bis);// restore the delegating input stream or the input streamif (is instanceof DelegatingInputStream) {((DelegatingInputStream)is).setInputStream(bis);} else {message.setContent(InputStream.class, bis);}bos.close();} catch (Exception e) {throw new Fault(e);}finally{LOGGER.info(bos.toString());}}}@Overrideprotected Logger getLogger() {// TODO Auto-generated method stubreturn null;}
}

接口

package com.xbsafe.webservice.service;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService
public interface DemoService {@WebMethodpublic String test(@WebParam(name="param") String param);
}
© 2019 GitHub, Inc.

接口实现类

public class DemoServiceImpl implements DemoService {@Overridepublic String test(String param) {return "webservice demo get param:"+param;}
}

测试:
在这里插入图片描述
注意事项:

springboot在配置webservice之后发现原来在controller里面写的get或post接口不能访问了,原因是springboot默认注册的是 dispatcherServlet,当

手动配置 ServletRegistrationBean 之后便不会再去注册默认的dispatcherServlet,此时需要手动去注册一个dispatcherServlet,代码如下:

/*** 注册一个dispatcherServlet,解决增加ws之后http接口访问不了问题*/@Beanpublic ServletRegistrationBean restServlet(){//注解扫描上下文AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();//base packageapplicationContext.scan("com.xbsafe");//通过构造函数指定dispatcherServlet的上下文DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);//用ServletRegistrationBean包装servletServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);registrationBean.setLoadOnStartup(1);//指定urlmappingregistrationBean.addUrlMappings("/*");//指定name,如果不指定默认为dispatcherServletregistrationBean.setName("rest");return registrationBean;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/249958.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Django】认证系统

目录 #. auth模块1. 认证 authenticate()2. 登陆 login(HttpRequest, user)3. 注销 logout(request)4. 认证判断 is_authenticated()5. 登陆校验 login_requierd()6. 创建普通用户 create_user()7. 创建超级用户 create_superuser()8. 密码校验 check_password(password)9. 修改…

学习的目的是什么?

学习的目的是为了掌握生存的常识和技能&#xff0c;以便独立地面对世界&#xff1b; 学习的目的是为了遵从生活的规范和律则&#xff0c;以便和谐地与人相处&#xff1b; 学习的目的是为了探索生命的价值和意义&#xff0c;以便有尊严地立于天地之间。 你觉得为什么要学习呢&am…

span里面插入文字

.text-box span::before{ content:attr(data-text);} 转载于:https://www.cnblogs.com/smzd/p/8491664.html

Spring Boot 动态注入的两种方式

通过Profilespring.profiles.active spring.profiles.active&#xff1a;官方解释是激活不同环境下的配置文件&#xff0c;但是实际测试发现没有对应的配置文件也是可以正常执行的。那就可以把这个key当作一个参数来使用 Profile&#xff1a;spring.profiles.active中激活某配…

kernel devel 安装与卸载

1、查看系统内核 uname -r 2、查看已安装kernel-devel uname -a ; rpm -qa kernel\* | sort 3、下载对应的rpm wget xxx/kernel-devel-2.6.32-754.el6.x86_64.rpm 或者 $ sudo yum install "kernel-devel-uname-r $(uname -r)" 4、卸载已安装的内核 yum remove ker…

弹性布局

/* 开启弹性布局的换行 */ flex-wrap: wrap;/* 变为多行了 无法使用 align-items 进行位置设置 align-content 在多行的时候 设置属性 跟 justify-content 一模一样如果只有 一行时 无法生效 *//* 调整元素 在主轴上的 排布方式flex-end 到主轴的末尾flex-start 默认值center…

详解 vue-cli 的打包配置文件代码(给大家写写注释)

一、前言 对于webpack基础不好&#xff0c;node指令不通的童鞋。估计对自己搭建Vue、react脚手架是相当头疼的&#xff0c;有种无从下手的感觉。然而&#xff0c;从头看这2块&#xff0c;耗时太长&#xff0c;而且说实话得练才行&#xff0c;不练练手看不明白。那大多数人就采取…

AutoFac自动注入时报错

错误描述&#xff1a;An error occurred during the activation of a particular registration 解决办法&#xff1a;看到了particular这个单词&#xff0c;用我蹩脚的英语&#xff0c;估计是部分类&#xff1f;结合报错的两个类存在互相引用&#xff0c;这就明白了&#xff0c…

尝试修改源码需要用到git存一下

git reflog查看本地记录 git reset --hard 02a3260 转载于:https://www.cnblogs.com/smzd/p/8492065.html

poj3713 Transferring Sylla 枚举+tarjan判割点

其实就是判断是否为三连通图 三连通图指的是去掉3个点就不连通的图&#xff0c;但是并没有直接求三连通的算法。著名的Tarjan算法可以求解连通和割点&#xff0c;再枚举删除一个点就能达到三连通的目的。 先看用例2&#xff0c;是由用例1去掉一条边而变成非三连通图的&#xff…

html标签大全

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>This is study note</title><base href"我是做外链的&#xff0c;一般在head里面" target"_blank"><style type&q…

布尔文本搜索

select text from products where Match(# column) Against(#condition bool* IN BOOLEAN MODE); 布尔操作符     包含 必须存在 -    排除,必须不存在(即使包含其他指定的词也不返回) >    增加排序等级 <    降低排序等级 ()    把词组成子表达式 …

Linux 安装Zookeeper单机版(使用Mac远程访问)

阅读本文需要先阅读安装Zookeeper<准备> 新建目录 mkdir /usr/local/zookeeper 解压 cd zookeeper压缩包所在目录 tar -xvf zookeeper-3.4.12.tar.gz -C /usr/local/zookeeper 新建目录 mkdir /usr/local/zookeeper/zookeeper-3.4.12/data 配置文件准备 cp /usr/local/zo…

深入vue

转载于:https://www.cnblogs.com/smzd/p/8547748.html

java全栈

前端&#xff1a;HTML/HTML5、CSS/CSS3、Javascript、jQuery、RequireJS、AngularJS、Vue 后端&#xff1a;Java、Struts2/Spring MVC、JPA/Mybatis、Spring Boot 安全&#xff1a;Shiro、Spring Security 中间件&#xff1a;Dubbo、ActiveMQ/RabbitMQ、Nginx 数据库&#…

vue与webpack

由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西&#xff0c;开发过程中需要改动到build和config里面一些相关的配置&#xff0c;所以刚好趁此机会将所有配置文件看一遍&#xff0c;理一理思路&#xff0c;也便于以后修改配置的时候不会“太折腾”。 Vue-webpack项…

size_t为什么重要

参考&#xff1a;https://www.zhihu.com/question/24773728/answer/66535663前言&#xff1a;使用size_t可能会提高代码的可移植性、有效性或者可读性&#xff0c;或许同时提高这三者。在标准C库中的许多函数使用的参数或者返回值都是表示的用字节表示的对象大小&#xff0c;比…

html--form表单常用操作

form表单 用于收集用户信息&#xff0c;如&#xff1a;登录、注册等场景&#xff1b;所有要提交的数据都必须放在form标签中<form action" " method" "> action&#xff1a;提交地址、动作&#xff0c;与input标签中type标签的submit属性相关联。 &…

MySQL触发器(转载)

触发器&#xff08;trigger&#xff09;是数据库中的一个很重要的、很实用的基于事件的处理器&#xff0c;在处理一些业务需求的时候&#xff0c;使用触发器会很方便。似乎在《高性能MySQL》中&#xff0c;对触发器作了一定的描述&#xff0c;也提到使用中的一些优势和局限性&a…

神级bug解决方法

真的是神级bug,util包中的Arrays类导入不了&#xff0c;一直报错。原因&#xff1a;JDK 1.8和Myeclipse 8.5不兼容&#xff0c;导致java.util.Arrays类无法被编译。所以报错。解决方法&#xff1a;1.降低jdk版本。2.升高Myeclipse版本转载于:https://www.cnblogs.com/yanlongw/…