cxf+webservice基本配置及java.lang.ClassCastException: 服务端实体类 cannot be cast to 客户端实体类错误解决

本文主要是介绍cxf+webservice基本配置及java.lang.ClassCastException: 服务端实体类 cannot be cast to 客户端实体类错误解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

**

cxf+webservice基本配置及java.lang.ClassCastException: 服务端实体类 cannot be cast to 客户端实体类错误解决**

例如:java.lang.ClassCastException: com.xima.webservice.service.User cannot be cast to com.xima.breast.vo.User

其中User为wenservice传输的bean,两端bean中的属性相同

此处使用springboot,所用pom如下

         <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- http --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.41</version></dependency><!-- 热部署模块 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional>  <!--这个需要为 true 热部署才有效 --></dependency><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.1.11</version></dependency>
先将基本代码附上:

webservice接口:

@WebService(targetNamespace="http://service.webservice.modular.anyu.com")
public interface TestUserService {@WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。@WebMethodList<User> getUserByAll();
}

webservice接口实现类:

@WebService(serviceName="TestUserService",//对外发布的服务名targetNamespace="http://service.webservice.modular.anyu.com",//指定你想要的名称空间,通常使用使用包名反转endpointInterface="com.anyu.modular.webservice.service.TestUserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Component
public class TestUserServiceImpl implements TestUserService{@AutowiredBreastCureNodeDao breastCureNodeDao;//dao层,数据库查询public List<User> getUserByAll(){return breastCureNodeDao.selectUserByAll();}}

BreastCureNode 类 (bean):

@XmlRootElement(name="User")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder= {""})
public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String userName;private Integer age;private String six;private String school;private Date brithday;//.......get   ....set  ....toString省略}

webservice配置config

@Configuration
public class CxfConfig {@Autowiredprivate Bus bus;@AutowiredTestUserService testUserService;/*** 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问* wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl* 不用此方法,则访问 : http://127.0.0.1:8080/services/user?wsdl*/
//	@SuppressWarnings("all")
//	@Bean
//	public ServletRegistrationBean dispatcherServlet() {
//		return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
//	}/*** 方法一*/// @Bean(name = Bus.DEFAULT_BUS_ID)// public SpringBus springBus() {// return new SpringBus();// }//// @Bean// public TestUserService testUserService() {// return new TestUserServiceImpl();// }//// @Bean// public Endpoint endpoint() {// EndpointImpl endpoint = new EndpointImpl(springBus(), testUserService());// endpoint.publish("/user");// return endpoint;// }/** * 方法二* 若有多个webservice,将下面方法复制,修改service和publish即可 **/@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(bus, testUserService);endpoint.publish("/user/breast");endpoint.getInInterceptors().add(new AuthInterceptor());//添加用户密码验证,去掉则客户端不需要用户密码验证return endpoint;}}

AuthInterceptor 用户名密码验证:

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>{private static final Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);private SAAJInInterceptor saa = new SAAJInInterceptor();private static final String USER_NAME = "admin";private static final String USER_PASSWORD = "admin";public AuthInterceptor() {super(Phase.PRE_PROTOCOL);getAfter().add(SAAJInInterceptor.class.getName());}@Overridepublic void handleMessage(SoapMessage message) throws Fault {SOAPMessage mess = message.getContent(SOAPMessage.class);if (mess == null) {saa.handleMessage(message);mess = message.getContent(SOAPMessage.class);}SOAPHeader head = null;try {head = mess.getSOAPHeader();} catch (Exception e) {logger.error("getSOAPHeader error: {}",e.getMessage(),e);}if (head == null) {throw new Fault(new IllegalArgumentException("找不到Header,无法验证用户信息"));}NodeList users = head.getElementsByTagName("username");NodeList passwords = head.getElementsByTagName("password");if (users.getLength() < 1) {throw new Fault(new IllegalArgumentException("找不到用户信息"));}if (passwords.getLength() < 1) {throw new Fault(new IllegalArgumentException("找不到密码信息"));}String userName = users.item(0).getTextContent().trim();String password = passwords.item(0).getTextContent().trim();//可以从数据库获取用户名密码验证,也可以使用固定的用户名,密码//根据项目需要选择//固定的用户名和密码   此处为admin adminif(USER_NAME.equals(userName) && USER_PASSWORD.equals(password)){logger.debug("admin auth success");} else {SOAPException soapExc = new SOAPException("认证错误");logger.debug("admin auth failed");throw new Fault(soapExc);}}
}

客户端代码:

public class CxfClient {public static void main(String[] args) {CxfClient.main1();CxfClient.main2();}/*** 1.代理类工厂的方式,需要拿到对方的接口地址*/public static void main1() {try {// 接口地址String address = "http://192.168.0.100:8081/services/user/breast?wsdl";// 代理工厂JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();// 设置代理地址jaxWsProxyFactoryBean.setAddress(address);// 设置接口类型jaxWsProxyFactoryBean.setServiceClass(TestUserService.class);// 创建一个代理接口实现TestUserService us = (TestUserService) jaxWsProxyFactoryBean.create();// 数据准备// 调用代理接口的方法调用并返回结果String result = us.getUserByAll();System.err.println("返回结果:" + result);} catch (Exception e) {e.printStackTrace();}}/*** 2:动态调用*/public static void main2() {// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://192.168.0.100:8081/services/user/breast?wsdl");// 需要密码的情况需要加上用户名和密码client.getOutInterceptors().add(new ClientLoginInterceptor("admin", "admin"));Object[] objects = new Object[0];try {// invoke("方法名",参数1,参数2,参数3....);objects = client.invoke("getUserByAll");List<User> users = (List<User>)objects[0];for (User user : users) {System.out.println(user.toString());}System.err.println("main2 : 动态调用  : 返回数据:" + objects[0]);} catch (java.lang.Exception e) {e.printStackTrace();}}
}

客户端User类:

public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String userName;private Integer age;private String six;private String school;private Date brithday;//.......get   ....set  ....toString省略}

ClientLoginInterceptor 验证用户名,密码的类:

public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{public ClientLoginInterceptor(String username, String password) {super(Phase.PREPARE_SEND);  this.username = username;this.password = password;}private String username;  private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void handleMessage(SoapMessage soap) throws Fault {List<Header> headers = soap.getHeaders();Document doc = DOMUtils.createDocument();Element auth = doc.createElement("authrity");Element username = doc.createElement("username");Element password = doc.createElement("password");username.setTextContent(this.username);password.setTextContent(this.password);auth.appendChild(username);auth.appendChild(password);headers.add(0, new Header(new QName("tiamaes"),auth));}}

此时基本代码书写完成,启动服务端,输入:http://localhost:8081/services/user/breast?wsdl则可以出现如下页面:

在这里插入图片描述

启动客户端: 则会报以下错误:

java.lang.ClassCastException: com.xima.webservice.service.User cannot be cast to com.xima.breast.vo.Userat com.example.demo.CxfClient.main2(CxfClient.java:79)at com.example.demo.CxfClient.main(CxfClient.java:26)
错误原因:

两个bean类的路径不同,导致映射失败;

将客户端User的路径改为com.xima.webservice.service.User即成功在客户端输出User数据

重点说明:bean类所在路径必须是wenservice的路径

这篇关于cxf+webservice基本配置及java.lang.ClassCastException: 服务端实体类 cannot be cast to 客户端实体类错误解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/410800

相关文章

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

如何合理管控Java语言的异常

《如何合理管控Java语言的异常》:本文主要介绍如何合理管控Java语言的异常问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、Thorwable类3、Error4、Exception类4.1、检查异常4.2、运行时异常5、处理方式5.1. 捕获异常

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4