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 {

    @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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM