學習使用webservice【我】


 

主要參考如下大神文章:

 

一個網上可用的免費webservice

http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

 

WebService客戶端調用常見5種方式

 

按照文章搭建服務端,

遇到

坑1:上面這個Endpoint相關類會報錯

   @Bean
    public Endpoint endpoint(UserService userService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
        endpoint.publish("/user");// 發布地址
        return endpoint;
    }

上面這個Endpoint相關類會報錯,主要是導入的包不對,下面貼這個類的完整代碼:

package com.learn.simplewebserviceserver.config;

import com.learn.simplewebserviceserver.webservice.UserService;
import com.learn.simplewebserviceserver.webservice.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws22.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 CxfWebServiceConfig {

    // 這里需要注意 若beanName命名不是 cxfServletRegistration 時,會創建兩個CXFServlet的。
    // 具體可查看下自動配置類:Declaration
    // org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
    // 也可以不設置此bean 直接通過配置項 cxf.path 來修改訪問路徑的
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean<CXFServlet> cxfServletRegistration() {
        // 注冊servlet 攔截/ws 開頭的請求 不設置 默認為:/services/*
        return new ServletRegistrationBean<CXFServlet>(new CXFServlet(), "/ws/*");
    }

    /**
     * 申明業務處理類 當然也可以直接 在實現類上標注 @Service
     *
     */
    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }

    /*
     * 非必要項
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        SpringBus springBus = new SpringBus();
        return springBus;
    }

    /*
     * 發布endpoint
     */
    @Bean
    public Endpoint endpoint(UserService userService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
        endpoint.publish("/user");// 發布地址
        return endpoint;
    }
}

 

坑2:啟動項目時報錯,起不來項目:

2020-07-22 15:35:10.169  WARN 16540 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration': Unsatisfied dependency expressed through field 'properties'; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'cxf-org.apache.cxf.spring.boot.autoconfigure.CxfProperties': Could not bind properties to 'CxfProperties' : prefix=cxf, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
2020-07-22 15:35:10.169  INFO 16540 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-07-22 15:35:10.174  INFO 16540 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-07-22 15:35:10.187  INFO 16540 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-22 15:35:10.194 ERROR 16540 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Bean Validation API is on the classpath but no implementation could be found

Action:

Add an implementation, such as Hibernate Validator, to the classpath

 

在pom文件中添加 Hibernate Validator 相關jar包:

      <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>

 

---

調用也可以參考下面文章:

 

WebService的四種客戶端調用方式(基本)

 

。。。。

自己測試時,如果按照第一個文檔自己發布的服務,然后用jdk自帶工具生成客戶端代碼的方式可以使用如下代碼進行測試:

package com.example.webserviceclient.webservice;

import com.example.webserviceclient.genclientcode.UserDto;
import com.example.webserviceclient.genclientcode.UserService;
import com.example.webserviceclient.genclientcode.UserService_Service;
import com.example.webserviceclient.tq.MobileCodeWS;
import com.example.webserviceclient.tq.MobileCodeWSSoap;

public class UserClient {
    public static void main1(String[] args) {
        //http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
        //創建服務訪問點集合的對象,例如:<wsdl:service name="MobileCodeWS">
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        //獲取服務實現類,例如:<wsdl:portType name="MobileCodeWSSoap">,port--binding--portType
        //MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);
        //根據服務訪問點的集合中的服務訪問點的綁定對象來獲得綁定的服務類
        //獲得服務類的方式是get+服務訪問點的name:getWSServerPort
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("18518114962", "");
        System.out.println(mobileCodeInfo);
    }

    public static void main(String[] args) {
        UserService_Service userService_service = new UserService_Service();
        UserService userPortName = userService_service.getUserPortName();
        UserDto u = userPortName.getUserByName("王五");
        System.out.println(u);
    }


}

會報錯,還沒解決

 


免責聲明!

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



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