hjr-SpringBoot: webservice

如果你用springboot時遇到了webservice啓動成功了,但是其他的接口卻變404了
配置文件可以參考下文

webservice有別於控制器裏的api
同一個端口,可以同時發佈webservice服務和普通api

可以這麼建項目

com.xxx.xxx
–controller
–webservice

其中controller文件夾裏都是api接口
webservice文件夾裏都是webservice服務和一個配置文件

webservie中
一個接口,一個實現,就是一個服務,然後還有一個配置文件,可以是xml配置文件,可以是。java配置文件,目的都是爲了bean的注入。

如果是用springboot 建議用.java配置文件的方式注入bean

和普通api的方式一樣,先寫接口,再寫實現

@WebService(name = "DemoService", // 暴露服務名稱
        targetNamespace = "http://xxx.xxx.com/xxx"// 命名空間,一般是接口的包名倒序
)
public interface DataSynchronizeService {  =
    public String test(String testXML);

}
import javax.jws.WebService;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

@WebService(serviceName = "DemoService", // 與接口中指定的name一致
        targetNamespace = "http://xxx.xxx.com/xxx", // 與接口中的命名空間一致,一般是接口的包名倒
        endpointInterface = "包路徑.DataSynchronizeService"// 接口地址
)
public class  DataSynchronizeServiceImpl implements DataSynchronizeService{
    @Override
    public String test(String testXML){
  		//對xml的處理
        return  "成功";
    }
}

配置類

@Configuration
public class WebServiceConfig {
	//此處對bean方法的命名 必須用這個,否則其他普通接口會變成404
    @Bean
    public ServletRegistrationBean disServlet(){
        return new ServletRegistrationBean(new CXFServlet() , "/service/*");
    }

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

    @Bean
    public DataSynchronizeServiceImpl DataSynchronizeService(){
        return  new DataSynchronizeServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), DataSynchronizeService());//綁定要發佈的服務
        endpoint.publish("/api"); //顯示要發佈的名稱
        return endpoint;
    }
}

啓動項目,最後在瀏覽器輸入 ip:port/service/api?xsdl 查看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章