今天学习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
);
改正后,错误问题得以解决。