导入依赖的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. Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

    D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...

  2. ajax传递数组及后台接收

    ajax传递的是{"items":arr},其中arr=[]; 在后台String[] items=req.getParameterValues("items" ...

  3. intval()

    1.将字符串转换成整数 2.取数字的整数部分

  4. 训练超参数, 出现 Cannot use GPU in CPU-only Caffe 错误?

    当我们用MNIST手写体数字数据库和LeNet CNN 模型训练超参数,运行 examples/mnist/train_lenet.sh是出现Cannot use GPU in CPU-only Ca ...

  5. Java 设计模式系列(十五)迭代器模式(Iterator)

    Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...

  6. Java Thread系列(九)Master-Worker模式

    Java Thread系列(九)Master-Worker模式 Master-Worker模式是常用的并行设计模式. 一.Master-Worker 模式核心思想 Master-Worker 系统由两 ...

  7. 【美食技术】家庭自制DIY鸡蛋饼和疙瘩汤早餐视频教程

    鸡蛋饼制作方法 食材准备面粉 150g鸡蛋饼  鸡蛋饼鸡蛋 2个盐 适量水 适量(约300ml)油 20g荵花适量也可根据自己喜好准备一些调味料. 做法 鸡蛋饼是一种家常点心,做法很多,这里提供3种. ...

  8. jdbc注册驱动 class.forName()

    从源码 D:\Javasoftware\MySql\mysql\mysql-connector-java-5.1.7\src\com\mysql\jdbc\Driver.java class.forN ...

  9. RECONSUME_LATER

    Failure consumption,later try to consume. ================MessageExt [queueId=0, storeSize=134, queu ...

  10. swift的可选值(optional)

    苹果那文档写了一大堆也没有好好的写一下可选值(optional)这个东西.就是在有一个“Optional Chaining”的章节,但是也不是很充分的说明.最后找了半天在“the basics”里墨迹 ...