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 { @Bean
public 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();
} @Bean
public DemoService demoJsonService(){
return new DemoServiceImpl();
} @Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoJsonService());
endpoint.publish("/ws");
endpoint.getInInterceptors().add(new WsInterceptor()); //add webservice inteceptor
return 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);
} @Override
public 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 later
InputStream 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 rest
IOUtils.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 stream
if (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());
}
}
} @Override
protected Logger getLogger() {
// TODO Auto-generated method stub
return null;
}
}
创建接口类(webservice的方法)
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface DemoService {
@WebMethod
public String test(@WebParam(name="param") String param);
}
接口实现类
public class DemoServiceImpl implements DemoService {
@Override
public String test(String param) {
return "webservice demo get param:"+param;
}
}
测试:

注意事项:
springboot在配置webservice之后发现原来在controller里面写的get或post接口不能访问了,原因是springboot默认注册的是 dispatcherServlet,当
手动配置 ServletRegistrationBean 之后便不会再去注册默认的dispatcherServlet,此时需要手动去注册一个dispatcherServlet,代码如下:
/**
* 注册一个dispatcherServlet,解决增加ws之后http接口访问不了问题
*/
@Bean
public ServletRegistrationBean restServlet(){
//注解扫描上下文
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//base package
applicationContext.scan("com.xbsafe");
//通过构造函数指定dispatcherServlet的上下文
DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext); //用ServletRegistrationBean包装servlet
ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
registrationBean.setLoadOnStartup(1);
//指定urlmapping
registrationBean.addUrlMappings("/*");
//指定name,如果不指定默认为dispatcherServlet
registrationBean.setName("rest");
return registrationBean;
}
springboot 配置webservice接口的更多相关文章
- springboot配置health接口
springboot配置health接口 spring-boot-starter-actuator 健康监控配置及使用 这样是可以看到一些结果的 如果在配置文件中用了下面这个,也是可以生效的 # 不进 ...
- spring与cxf整合配置webservice接口(以jaxws:server的方式配置)
ps:最近项目需要跟其他系统做同步,需要使用webservice来提供接口给其他系统调用:临时抱佛脚赶紧去网上找了下资料,发现用Endpoint的方式发布接口好容易哦:赶紧写了个例子做验证,发布成功. ...
- webService 接口调用配置
1.配置XML文件,如果新建一个XML文件需要在applicationContext.xm里面配置一下 <import resource="cxf-client.xml" / ...
- python通过webservice接口实现配置下发
项目上要开发一个小工具,通过webservice接口实现配置下发,考虑到python的第三方库对soap的良好支持,果断决定用python来完成这一使命. Python的支持webservice的第三 ...
- day63-webservice 08.在web项目中配置带有接口的webservice服务
这个是配置带有接口的WebService的服务. http://localhost:8080/cxf-web-server/service 带有接口的实现类也给它做好了.jaxws:endpoint是 ...
- WebService:java配置类形式发布WebService接口及遇见的问题总结
配置WebService前需要以下依赖jar包 #版本只供参考,具体看项目 <dependency> <grouId>org.apache.cxf</grouId> ...
- springboot配置cxf
1.引入两个需要的jar <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf- ...
- Springboot整合webservice
Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...
- springboot整合WebService简单版
一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...
随机推荐
- Country Meow
Country Meow 和这基本一样 https://www.cnblogs.com/Fighting-sh/p/9809518.html #include<iostream> #inc ...
- Qt Pro file
Qt keys qmake Manual Building Common Project Types #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- ROS 消息发布器和订阅器Publisher, Subscriber
博客参考:https://www.2cto.com/kf/201705/639776.html 1.编写发布器节点节点(Node) 是指 ROS 网络中可执行文件.接下来,将会创建一个发布器节点(“t ...
- 从iOS 11 UI Kit中谈谈iOS 11的新变化
北京时间9月20日凌晨1点,iOS 11终于迎来了正式版的推送,相信各位小伙伴已经在第一时间进行了升级.iOS 11毫无疑问是一次大规模的系统更新,UI.系统内核.锁屏等多方面都进行了不同程度的改进. ...
- Java程序设计11——GUI设计与事件处理A
1.GUI设计 Java使用AWT和Swing类完成图形用户界面编程,AWT全称是Abstract Window Toolkit,即抽象窗口工具集,它是Sun最早提供的GUI库,只是这个库功能比较有限 ...
- UVa 506 System Dependencies (细节问题)
题意:输入几种指令,让你进行模拟操作,指令如下: DEPEND item1 item2 (item3 ...) 安装item1需要先安装item2(.item3……) INSTALL item1 安装 ...
- mongodb-win32-i386-3.0.6 使用
一.下载地址 https://fastdl.mongodb.org/win32/mongodb-win32-i386-3.0.6.zip 二.安装 1. systeminfo OS 名称: Micro ...
- 创建圆角 抛出一个错误:二元运算符“|”不能用于两个UIRectCorner操作数
// let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRect ...
- VS2017中对C++的单元测试
安装Visual Studio 2017 由于平时都是用codeblock,因此电脑中没有装VS系列的IDE,就从安装开始吧 最开始安装的时候没有注意什么都没选,安装完了以后根本没有c++的编译器和各 ...
- 解决PL/SQL Developer连接数据库时出现 “ORA-12541:TNS:无监听程序”错误
在用PL/SQL Developer连接数据库时出现“ORA-12541:TNS:无监听程序”错误. 1.检查listener.log日志发现下面错误:TNSLSNR for 32-bit Windo ...