SpringBoot+webservice

今天看到一個項目要和工廠的ERP進行對接,用到了webservice。雖然使用用springboot較爲方便,仍是瞭解一下:java

webservice是什麼

網上的解釋不少,其實就是跨語言和操做系統的的遠程調用技術。好比亞馬遜,能夠將本身的服務以webservice的服務形式暴露出來,咱們就能夠經過web調用這些,不管咱們使用的語言是java仍是c,這也是SOA應用一種表現形式。web

WSDL(Web Services Description Language)將不管用何種語言書寫的web service描述出來,好比其參數或返回值。WSDL是服務端和客戶端都能解讀的標準格式。客戶端經過URL地址訪問到WSDL文件,在調用服務端以前先訪問WSDL文件。 讀取到WSDL後經過客戶端的API類能夠生成代理類,調用這些代理類就能夠訪問webservice服務。代理類將客戶端的方法變爲soap(Simple Object Access Protocol,能夠理解爲http+xml)格式經過http發送,同時接受soap格式的返回值並解析。spring


webservice使用

  • 建立工程

  • 添加依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>
複製代碼
  • 測試對象TestBean

public class TestBean {
        private String name;
        private String id;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }
複製代碼
  • 服務端接口

@WebService(name = "testService",targetNamespace = "http://service.webservicedemo.sxt.com")
    public interface Wbservice {

        @WebMethod
        @WebResult()
        public String helloService(@WebParam(name = "name") String name);
        
        @WebMethod
        public ArrayList<TestBean> getAllBean() } 複製代碼
  • 服務端接口實現

@WebService(name = "testService",
            targetNamespace = "http://service.webservicedemo.sxt.com",
            endpointInterface="com.sxt.webservicedemo.Service.Wbservice")

    @Component
    public class WebserviceImpl implements Wbservice {
        @Override
        public String helloService(String name) {
            return "hello"+name;
        }

        @Override
        public ArrayList<TestBean> getAllBean() {
            ArrayList<TestBean> list = new ArrayList<>();
            TestBean bean1 = new TestBean("zhangsan", "1");
            TestBean bean2 = new TestBean("lisi", "2");
            list.add(bean1);
            list.add(bean2);
            return list;
        }
    }
複製代碼
  • 發佈配置

@Configuration
public class Webserviceconfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/service/*");//發佈服務名稱
    }

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

    @Bean
    public Wbservice beanService() {
        return  new WebserviceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint=new EndpointImpl(springBus(), beanService());//綁定要發佈的服務
        endpoint.publish("/test"); //顯示要發佈的名稱
        return (Endpoint) endpoint;
    }
複製代碼
  • 客戶端調用

public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();

        Client client=dcflient.createClient("http://localhost:8080/service/test?wsdl");

        Object[] objects=client.invoke("getBean","411001");
        System.out.println("*******"+objects[0].toString());
    }
複製代碼
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息