学习使用webservice【我】
学习 webservice 使用
2023-09-27 14:28:21 时间
主要参考如下大神文章:
一个网上可用的免费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); } }
会报错,还没解决
相关文章
- 多线程编程学习笔记——线程池(一)
- 李宏毅机器学习_8_1循环神经网络上
- 关于wcf,webservice,webapi或者其他服务或者接口有什么区别 WCF、WebAPI、WebService之间的区别 【转载】HTTP和SOAP完全就是两个不同的协议 WebService学习总结(一)——WebService的相关概念
- 路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) ASP.NET WebApi技术从入门到实战演练 C#面向服务WebService从入门到精通 DataTable与List<T>相互转换
- WebService学习概念总结
- 【学习笔记26】JavaScript字符串的方法
- 【学习笔记39】获取DOM标签对象
- 【步态识别】基于深度学习的步态识别系统的MATLAB仿真,包括ALEXNET,改进CNN,GOOGLENET
- 使用WSDL发布WebService(第二部分)简单对象访问协议——学习SOAP语法和使用SOAP发布WSDL
- (数据科学学习手札45)Scala基础知识
- webservice学习第二天
- webservice学习第一天
- 【转载】 强化学习中 采用 【首次访问的蒙特卡洛预测法】 的算法描述
- WebService另一种轻量级实现—Hessian 学习笔记
- ETL学习笔记之二:ETL与BI
- Android学习站点推荐
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
- SQL学习之高级联结(自联结、自然联结、外联接)