zoukankan      html  css  js  c++  java
  • 学习使用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);
        }
    
    
    }

    会报错,还没解决

  • 相关阅读:
    配置PL/SQL Developer连接Oracle数据库
    解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题
    解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList问题
    解决spring mybatis 整合后mapper接口注入失败
    如何解决Failed to start component [StandardEngine[Catalina].StandardHost[127.0.0.1].StandardContext[]]问题
    五毛的cocos2d-x学习笔记05-场景与场景动画,动作
    五毛的cocos2d-x学习笔记04-触摸点
    五毛的cocos2d-x学习笔记03-控件
    五毛的cocos2d-x学习笔记02-基本项目源码分析
    五毛的cocos2d-x学习笔记01-创建项目
  • 原文地址:https://www.cnblogs.com/libin6505/p/13361084.html
Copyright © 2011-2022 走看看