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