javax.xml.bind.UnmarshalException: Unexpected element

本文详细介绍了在整合CXF与Spring时遇到的javax.xml.bind.UnmarshalException异常的解决方法,包括配置文件的调整与注解的正确使用。通过将客户端接口注解与实际需求相匹配,最终成功解决了异常问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习cxf整合spring时,遇到javax.xml.bind.UnmarshalException: Unexpected element 异常,先看配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  ">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 通过配置文件实现 wss4j -->
	
	<bean id="myServicePasswordCallback" class="com.tds.ws.util.ServicePwdCallback" />
	<bean id="myClinetPasswordCallback" class="com.tds.ws.client.cxf.util.ClientPwdCallback" />
	<jaxws:endpoint 
	  id="helloWorldService" 
	  implementor="com.tds.ws.service.impl.HellowServiceImpl" 
	  address="/helloService" >
	  	<jaxws:inInterceptors>
  			<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
		  		<constructor-arg>
		  			<map>
		  				<entry key="action" value="UsernameToken" />
		  				<entry key="passwordType" value="PasswordDigest" />
		  				<entry key="passwordCallbackRef" value-ref="myServicePasswordCallback"/>
		  			</map>
		  		</constructor-arg>
  			</bean>
  			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	  	</jaxws:inInterceptors>
	  	
	  	<jaxws:outInterceptors>
	  		<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	  	</jaxws:outInterceptors>
	 </jaxws:endpoint>
	  
	  <!-- client -->
	  <bean id="clientWss4jOutInterceptor"
	  	class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
	  	<constructor-arg>
	  		<map>
  				<entry key="action" value="UsernameToken" />
  				<entry key="passwordType" value="PasswordDigest" />
  				<entry key="user" value="initUser" />
  				<entry key="passwordCallbackRef" value-ref="myClinetPasswordCallback" />
  			</map>
	  	</constructor-arg>
	  	</bean>
	  <jaxws:client id="helloWorldClient"
		  	address="http://127.0.0.1:8080/webApp/cxf/helloService"
		  	serviceClass="com.tds.ws.client.cxf.IHellowService" >
			<jaxws:outInterceptors>
				<ref bean="clientWss4jOutInterceptor"/>
			</jaxws:outInterceptors>
	 </jaxws:client>
</beans>
<!-- END SNIPPET: beans -->
 
其中红色部分为客户端接口,因为整个客户端都是从另外一个项目中移植过来的,所以虽然导入包被我修改过了,但是如下的注解也需要改过来,如下图:

@WebService(targetNamespace = "http://service.ws.tds.com/", name = "IHellowService")
@XmlSeeAlso({ObjectFactory.class})
public interface IHellowService {

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "selectMaxLongNameCustomer", targetNamespace = "http://service.ws.tds.com/", className = "com.tds.ws.cxf.service.SelectMaxLongNameCustomer")
    @WebMethod
    @ResponseWrapper(localName = "selectMaxLongNameCustomerResponse", targetNamespace = "http://service.ws.tds.com/", className = "com.tds.ws.cxf.service.SelectMaxLongNameCustomerResponse")
    public com.tds.ws.cxf.service.Customer selectMaxLongNameCustomer(
        @WebParam(name = "c1", targetNamespace = "")
        com.tds.ws.cxf.service.Customer c1,
        @WebParam(name = "arg1", targetNamespace = "")
        com.tds.ws.cxf.service.Customer arg1
    );

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "selectMaxAgeStudent", targetNamespace = "http://service.ws.tds.com/", className = "com.tds.ws.cxf.service.SelectMaxAgeStudent")
    @WebMethod
    @ResponseWrapper(localName = "selectMaxAgeStudentResponse", targetNamespace = "http://service.ws.tds.com/", className = "com.tds.ws.cxf.service.SelectMaxAgeStudentResponse")
    public com.tds.ws.cxf.service.Customer selectMaxAgeStudent(
        @WebParam(name = "arg0", targetNamespace = "")
        com.tds.ws.cxf.service.Customer arg0,
        @WebParam(name = "arg1", targetNamespace = "")
        com.tds.ws.cxf.service.Customer arg1
    );

改正后,错误问题得以解决。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值