当前位置:   article > 正文

访问webservice不带命名空间org.apache.cxf.interceptor.Fault: UnmarshallingError: 意外的元素 (uri:““, local:“arg0“)_springboot集成webservice忽略命名空间的校验

springboot集成webservice忽略命名空间的校验

 想要实现:客户端访问服务端时不带命名空间。

在百度上搜索,是给服务添加一个拦截器。代码如下:

  1. import org.apache.cxf.interceptor.Fault;
  2. import org.apache.cxf.message.Message;
  3. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  4. import org.apache.cxf.phase.Phase;
  5. import org.apache.cxf.service.model.ServiceInfo;
  6. import java.io.*;
  7. public class ServerNameSpaceInterceptor extends AbstractPhaseInterceptor<Message> {
  8. public ServerNameSpaceInterceptor()
  9. {
  10. super(Phase.RECEIVE);
  11. }
  12. @Override
  13. public void handleMessage(Message message) throws Fault {
  14. for (ServiceInfo si : message.getExchange().getService().getServiceInfos()) {
  15. si.setProperty("soap.force.doclit.bare",true); //这个就是忽略客户端不带命名空间的关键
  16. }
  17. }
  18. }

程序访问报错:org.apache.cxf.interceptor.Fault: UnmarshallingError: 意外的元素 (uri:"", local:"arg0")

个人解决方案:在上述代码中的handleMessage方法中添加获取请求流,将请求流转成字符串,处理字符串中命名空间、参数、方法名相关信息,参考代码如下:

  1. @Override
  2. public void handleMessage(Message message) throws Fault {
  3. for (ServiceInfo si : message.getExchange().getService().getServiceInfos()) {
  4. si.setProperty("soap.force.doclit.bare",true); //这个就是忽略客户端不带命名空间的关键
  5. }
  6. // 获取参数
  7. InputStream is = message.getContent(InputStream.class);
  8. BufferedReader in = new BufferedReader(new InputStreamReader(is));
  9. StringBuffer reqParamsBuf = new StringBuffer();
  10. String line = "";
  11. while (true) {
  12. try {
  13. if (!((line = in.readLine()) != null)) break;
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. //post参数,json格式
  18. reqParamsBuf.append(line);
  19. }
  20. String str = reqParamsBuf.toString();
  21. if(str.indexOf("xmlns:ns1") < 0){//特殊处理,请求中没有命名空间,处理请求流
  22. //根据提供服务的方法修改请求流
  23. //处理方法的命名空间
  24. str = str.replace("><arg0"," xmlns:ns1=\"service.mvc.web.w.a.com\"><arg0");
  25. //处理方法的参数个数
  26. str = str.replace("arg0", "ns1:arg0");
  27. str = str.replace("arg1", "ns1:arg1");
  28. //处理方法名
  29. str = str.replace("execute", "ns1:execute");
  30. }
  31. message.setContent(InputStream.class, new ByteArrayInputStream(str.getBytes()));
  32. }

所有代码:

pom依赖:

  1. <dependency>
  2. <groupId>org.apache.cxf</groupId>
  3. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  4. <version>3.4.4</version>
  5. </dependency>

代码结构:(package-info.java和接口同级目录)

webService的配置:webServiceConfig.java

  1. package com.a.walk.web.mvc.config;
  2. import com.a.walk.web.mvc.service.ServerNameSpaceInterceptor;
  3. import com.a.walk.web.mvc.service.TestWebService;
  4. import org.apache.cxf.Bus;
  5. import org.apache.cxf.bus.spring.SpringBus;
  6. import org.apache.cxf.jaxws.EndpointImpl;
  7. import org.apache.cxf.transport.servlet.CXFServlet;
  8. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import javax.xml.ws.Endpoint;
  12. @Configuration
  13. public class WebServiceConfig {
  14. /**
  15. * 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
  16. *
  17. * @return
  18. */
  19. @Bean
  20. public ServletRegistrationBean cxfServlet() {
  21. return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");//第二个参数:设置CXFServlet注册地址
  22. }
  23. @Bean(name = Bus.DEFAULT_BUS_ID)
  24. public SpringBus springBus() {
  25. return new SpringBus();
  26. }
  27. /**
  28. * 注册WebService接口到webservice服务
  29. *
  30. * @return
  31. */
  32. @Bean(name = "webServiceEndpoint")
  33. public Endpoint sweptPayEndpoint(TestWebService webService) {
  34. EndpointImpl endpoint = new EndpointImpl(springBus(), webService);
  35. endpoint.publish("/test");//设置接口注册地址
  36. endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor());
  37. return endpoint;
  38. }
  39. }

接口TestWebService.java:

  1. package com.a.w.web.mvc.service;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebResult;
  5. import javax.jws.WebService;
  6. @WebService(targetNamespace = "service.mvc.web.walk.ao.com",name = "testWebService")
  7. public interface TestWebService {
  8. @WebResult(name = "execute", targetNamespace = "service.mvc.web.walk.a.com")
  9. @WebMethod
  10. String execute(String funcName,String value);
  11. }

实现类TestWebServiceImpl.java:

  1. package com.a.w.web.mvc.service.impl;
  2. import com.a.w.web.mvc.service.TestWebService;
  3. import org.springframework.stereotype.Service;
  4. import javax.jws.WebMethod;
  5. import javax.jws.WebParam;
  6. import javax.jws.WebResult;
  7. import javax.jws.WebService;
  8. @Service
  9. @WebService(targetNamespace = "service.mvc.web.w.a.com", serviceName = "Services",endpointInterface = "com.a.w.web.mvc.service.TestWebService")
  10. public class TestWebServiceImpl implements TestWebService {
  11. @WebResult(name = "execute", targetNamespace = "service.mvc.web.w.as.com")
  12. @WebMethod
  13. public String execute(String funcName, String value) {
  14. return funcName;
  15. }
  16. }

package-info.java:

  1. @javax.xml.bind.annotation.XmlSchema(//namespace = "http://com.mypro.service",
  2. attributeFormDefault = XmlNsForm.QUALIFIED,
  3. elementFormDefault= XmlNsForm.QUALIFIED)
  4. package com.a.w.web.mvc.service;
  5. import javax.xml.bind.annotation.XmlNsForm;

拦截器ServerNameSpaceInterceptor.java:

  1. package com.a.w.web.mvc.service;
  2. import org.apache.cxf.interceptor.Fault;
  3. import org.apache.cxf.message.Message;
  4. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  5. import org.apache.cxf.phase.Phase;
  6. import org.apache.cxf.service.model.ServiceInfo;
  7. import java.io.*;
  8. public class ServerNameSpaceInterceptor extends AbstractPhaseInterceptor<Message> {
  9. public ServerNameSpaceInterceptor()
  10. {
  11. super(Phase.RECEIVE);
  12. }
  13. @Override
  14. public void handleMessage(Message message) throws Fault {
  15. for (ServiceInfo si : message.getExchange().getService().getServiceInfos()) {
  16. si.setProperty("soap.force.doclit.bare",true); //这个就是忽略客户端不带命名空间的关键
  17. }
  18. // 获取参数
  19. InputStream is = message.getContent(InputStream.class);
  20. BufferedReader in = new BufferedReader(new InputStreamReader(is));
  21. StringBuffer reqParamsBuf = new StringBuffer();
  22. String line = "";
  23. while (true) {
  24. try {
  25. if (!((line = in.readLine()) != null)) break;
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. //post参数,json格式
  30. reqParamsBuf.append(line);
  31. }
  32. String str = reqParamsBuf.toString();
  33. if(str.indexOf("xmlns:ns1") < 0){//特殊处理,请求中没有命名空间,处理请求流
  34. //根据提供服务的方法修改请求流
  35. //处理方法的命名空间
  36. str = str.replace("><arg0"," xmlns:ns1=\"service.mvc.w.w.a.com\"><arg0");
  37. //处理方法的参数个数
  38. str = str.replace("arg0", "ns1:arg0");
  39. str = str.replace("arg1", "ns1:arg1");
  40. //处理方法名
  41. str = str.replace("execute", "ns1:execute");
  42. }
  43. message.setContent(InputStream.class, new ByteArrayInputStream(str.getBytes()));
  44. }
  45. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/827767
推荐阅读
相关标签
  

闽ICP备14008679号