導入依賴的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;}