springboot 配置webservice接口

本是古典 何须时尚 2021-10-14 01:44 500阅读 0赞

导入依赖的jar

  1. <!-- webservice cxf -->
  2. <dependency>
  3. <groupId>org.apache.cxf</groupId>
  4. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  5. <version>3.1.6</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.cxf</groupId>
  9. <artifactId>cxf-rt-transports-http</artifactId>
  10. <version>3.1.6</version>
  11. </dependency>

创建webservice配置类

  1. import com.xbsafe.webservice.service.DemoService;
  2. import com.xbsafe.webservice.service.DemoServiceImpl;
  3. import org.apache.cxf.Bus;
  4. import org.apache.cxf.bus.spring.SpringBus;
  5. import org.apache.cxf.jaxws.EndpointImpl;
  6. import org.apache.cxf.transport.servlet.CXFServlet;
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import javax.xml.ws.Endpoint;
  11. @Configuration
  12. public class CxfConfig {
  13. @Bean
  14. public ServletRegistrationBean dispatcherServlet() {
  15. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/demo/*");
  16. servletRegistrationBean.setName("webService");
  17. return servletRegistrationBean;
  18. }
  19. @Bean(name = Bus.DEFAULT_BUS_ID)
  20. public SpringBus springBus() {
  21. return new SpringBus();
  22. }
  23. @Bean
  24. public DemoService demoJsonService(){
  25. return new DemoServiceImpl();
  26. }
  27. @Bean
  28. public Endpoint endpoint() {
  29. EndpointImpl endpoint = new EndpointImpl(springBus(), demoJsonService());
  30. endpoint.publish("/ws");
  31. endpoint.getInInterceptors().add(new WsInterceptor()); //add webservice inteceptor
  32. return endpoint;
  33. }
  34. }

创建webservice拦截器类

  1. import org.apache.cxf.helpers.IOUtils;
  2. import org.apache.cxf.interceptor.AbstractLoggingInterceptor;
  3. import org.apache.cxf.interceptor.Fault;
  4. import org.apache.cxf.io.CachedOutputStream;
  5. import org.apache.cxf.io.DelegatingInputStream;
  6. import org.apache.cxf.message.Message;
  7. import org.apache.cxf.phase.Phase;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.InputStream;
  10. import java.io.SequenceInputStream;
  11. import java.util.logging.Logger;
  12. public class WsInterceptor extends AbstractLoggingInterceptor {
  13. private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(WsInterceptor.class);
  14. public WsInterceptor() {
  15. super(Phase.RECEIVE);
  16. }
  17. @Override
  18. public void handleMessage(Message message) throws Fault {
  19. InputStream is = message.getContent(InputStream.class);
  20. if(is!=null){
  21. CachedOutputStream bos = new CachedOutputStream();
  22. if (threshold > 0) {
  23. bos.setThreshold(threshold);
  24. }
  25. try {
  26. // use the appropriate input stream and restore it later
  27. InputStream bis = is instanceof DelegatingInputStream
  28. ? ((DelegatingInputStream)is).getInputStream() : is;
  29. //only copy up to the limit since that's all we need to log
  30. //we can stream the rest
  31. IOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);
  32. bos.flush();
  33. bis = new SequenceInputStream(bos.getInputStream(), bis);
  34. // restore the delegating input stream or the input stream
  35. if (is instanceof DelegatingInputStream) {
  36. ((DelegatingInputStream)is).setInputStream(bis);
  37. } else {
  38. message.setContent(InputStream.class, bis);
  39. }
  40. bos.close();
  41. } catch (Exception e) {
  42. throw new Fault(e);
  43. }finally{
  44. LOGGER.info(bos.toString());
  45. }
  46. }
  47. }
  48. @Override
  49. protected Logger getLogger() {
  50. // TODO Auto-generated method stub
  51. return null;
  52. }
  53. }

接口

  1. package com.xbsafe.webservice.service;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. @WebService
  6. public interface DemoService {
  7. @WebMethod
  8. public String test(@WebParam(name="param") String param);
  9. }
  10. © 2019 GitHub, Inc.

接口实现类

  1. public class DemoServiceImpl implements DemoService {
  2. @Override
  3. public String test(String param) {
  4. return "webservice demo get param:"+param;
  5. }
  6. }

测试:
在这里插入图片描述
注意事项:

springboot在配置webservice之后发现原来在controller里面写的get或post接口不能访问了,原因是springboot默认注册的是 dispatcherServlet,当

手动配置 ServletRegistrationBean 之后便不会再去注册默认的dispatcherServlet,此时需要手动去注册一个dispatcherServlet,代码如下:

  1. /** * 注册一个dispatcherServlet,解决增加ws之后http接口访问不了问题 */
  2. @Bean
  3. public ServletRegistrationBean restServlet(){
  4. //注解扫描上下文
  5. AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
  6. //base package
  7. applicationContext.scan("com.xbsafe");
  8. //通过构造函数指定dispatcherServlet的上下文
  9. DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
  10. //用ServletRegistrationBean包装servlet
  11. ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
  12. registrationBean.setLoadOnStartup(1);
  13. //指定urlmapping
  14. registrationBean.addUrlMappings("/*");
  15. //指定name,如果不指定默认为dispatcherServlet
  16. registrationBean.setName("rest");
  17. return registrationBean;
  18. }

发表评论

表情:
评论列表 (有 0 条评论,500人围观)

还没有评论,来说两句吧...

相关阅读