日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 配置webservice接口

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 配置webservice接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的springboot 配置webservice接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。