导入依赖的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;
}

完整代码地址:https://github.com/gulang-jx/exercise/tree/master/springbootMutlDS/src/main/java/com/xbsafe/webservice

springboot 配置webservice接口的更多相关文章

  1. springboot配置health接口

    springboot配置health接口 spring-boot-starter-actuator 健康监控配置及使用 这样是可以看到一些结果的 如果在配置文件中用了下面这个,也是可以生效的 # 不进 ...

  2. spring与cxf整合配置webservice接口(以jaxws:server的方式配置)

    ps:最近项目需要跟其他系统做同步,需要使用webservice来提供接口给其他系统调用:临时抱佛脚赶紧去网上找了下资料,发现用Endpoint的方式发布接口好容易哦:赶紧写了个例子做验证,发布成功. ...

  3. webService 接口调用配置

    1.配置XML文件,如果新建一个XML文件需要在applicationContext.xm里面配置一下 <import resource="cxf-client.xml" / ...

  4. python通过webservice接口实现配置下发

    项目上要开发一个小工具,通过webservice接口实现配置下发,考虑到python的第三方库对soap的良好支持,果断决定用python来完成这一使命. Python的支持webservice的第三 ...

  5. day63-webservice 08.在web项目中配置带有接口的webservice服务

    这个是配置带有接口的WebService的服务. http://localhost:8080/cxf-web-server/service 带有接口的实现类也给它做好了.jaxws:endpoint是 ...

  6. WebService:java配置类形式发布WebService接口及遇见的问题总结

    配置WebService前需要以下依赖jar包 #版本只供参考,具体看项目 <dependency> <grouId>org.apache.cxf</grouId> ...

  7. springboot配置cxf

    1.引入两个需要的jar <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf- ...

  8. Springboot整合webservice

    Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...

  9. springboot整合WebService简单版

    一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...

随机推荐

  1. gorm中自己写sql的方法实现

    type Result struct { Total int } var result Result //当天修改作业的总时间:分钟 dao.DB(dao.HomeworkTable).Raw(&qu ...

  2. uva10288 Coupons 【概率 分数】

    题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...

  3. spring boot 1

    1.创建项目. meven可以配置阿里云meven镜像 <mirror> <id>nexus-aliyun</id> <mirrorOf>central ...

  4. Notebook computer(Ubuntu)

    ==============Mask_RCNN============== source activate flappbird cd /home/luo/Desktop/MyFile/MaskRCNN ...

  5. springMVC框架介绍以及运行流程(图解)

    1 Springmvc是什么? spring web mvc和struts2都属于表现层的框架,spring web mvc是spring框架的一部分(所以spring mvc与spring之间不需要 ...

  6. Warning: Data truncated for column 'xxxx' at row 1

    The problem was that I changed the column's length only in the program.I had to do either change the ...

  7. Caffe 议事(三):从零开始搭建 ResNet 之 网络的搭建(中)

    上面2个函数定义好了,那么剩下的编写网络就比较容易了,我们在ResNet结构介绍中有一个表,再贴出来: Layer_name Output_size 20-layer ResNet Conv1 32 ...

  8. PMP项目管理笔记

    项目管理三重制约:时间.成本.范围.结果:质量(结果质量和过程质量)四个层次:复杂事情简单化:分解简单事情量化:临界值量化的事情专业化:规律专业的事情模板化:框架模板 工作分日常运作和项目.日常运作为 ...

  9. mongo学习- group操作

    group可以使用 $sum,$avg,$max,$min,$first,$last

  10. cpuinfo

    在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 经过查看,我的开发机器是1个物理CP ...