导入依赖的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. war项目部署流程

    准备: 1安装jdk1.7及以上版本 2安装tomcat7及以上版本 到%tomcat%/bin目录下记事本编辑server.xml, 配置<Connector>元素port端口,及< ...

  2. learning.py报错

    在廖雪峰大神的网站下学习了Python,其中有一个提供互动环境的Python脚本--learning.py,报了个错,看了下源文件的代码,安排了一下. 报错信息: This learning.py i ...

  3. 有关jQuery valid 验证多个同name属性的input的问题

    今天遇到需要动态添加多个同name属性的input框,用jQuery valid进行验证时,没有指定不同的id,验证只会在第一个显示提示信息,因为jQuery valid显示的提示信息是根据每个inp ...

  4. ADF控件ID变化引发JS无法定位控件的解决方法

    原文地址:ADF控件ID变化引发JS无法定位控件的解决方法作者:Nicholas JSFF定义的控件ID到了客户端时往往会改变.例如在JSFF中的一个的ID为"ot1",但是当这个 ...

  5. win10系统的快捷键

    1.win10特有的快捷键:任务视图和虚拟桌面相关 (1)Win + Tab:查看任务视图 (2)Win + Ctrl + D:在任务视图中新建虚拟桌面 (3)Win + Ctrl + F4:关闭当前 ...

  6. sqlserver select 查询字段if判断用法

    SELECT TOP 1000 [id],      case when group_id>1 then 'vip'           else '普通会员'           end  F ...

  7. SetupDiEnumDeviceInfo

    BOOLEANSetupDiEnumDeviceInfo(IN HDEVINFO DeviceInfoSet,IN DWORD MemberIndex,OUT PSP_DEVINFO_DATA Dev ...

  8. Hibernate 基本概念

    这一段正在学Hibernate,首先要了解下Hibernate大概的意思,究竟什么是Hibernate,到底它是个什么东西,必须从整体上把握下Hibernate在整个开发过程中所起到的作用,这样对更深 ...

  9. linux安装mysql后root无法登录

    [root@localhost mysql]# mysql -u root -pEnter password: ERROR 1045 (28000): Access denied for user ' ...

  10. linux每天一小步---xargs命令详解

    1 命令功能 xargs用来从标准输入中执行命令行 xargs命令用来将一些不支持管道传递参数的命令而使之支持 2 命令语法 xargs  [选项参数] commands 3 命令参数 -O 当标准输 ...