package com.steerinfo.server.config;

import com.steerinfo.server.WebServer;
import com.steerinfo.server.serverimpl.WebServerimpl;
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 org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.xml.ws.Endpoint;

/**
 * @Description 不使用xml文件时这样配置
 * @Author fubo
 * @Date 2020/8/5 11:20
 **/
@Configuration
public class WebServiceConfig {
    //注册Servlet组件
    /**
     * 此方法作用是改变项目中服务名的前缀名，此处127.0.0.1或者localhost不能访问时，请使用ipconfig查看本机ip来访问
     * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/web?wsdl
     * 去掉注释后：wsdl访问地址为：http://127.0.0.1:8080/kexin/webservice/web?wsdl

     * @return
     */
    @SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean dispatcherServlet(){
        return  new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

    //Bus总线s
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }

    @Bean
    public WebServer WebServer(){
        return new WebServerimpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), WebServer());//绑定要发布的服务
        endpoint.publish("/limssubject");//显示要发布的名称
        return endpoint;
    }
    @Bean
    public ServletRegistrationBean restServlet() {
        //注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //项目包名
        //applicationContext.scan("com.steerinfo.*");
        DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/*");
        //一定要加上此行
        registrationBean.setName("rest_dispatcherServlet");
        return registrationBean;
    }
}
