自定义SOAP响应的JAX-WS前缀

20

目标

我正在实现一个针对相当古老(但很遗憾不能更改)接口的Web服务。我的问题是,调用我的服务的客户端期望在SOAP响应中有特定的命名空间,并且我很难将其更改为匹配。

考虑一个hello world的示例,我希望得到这个:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:helloResponse xmlns:ns2="http://test/">
         <return>Hello Catchwa!</return>
      </ns2:helloResponse>
   </S:Body>
</S:Envelope>

要看起来像这样:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <customns:helloResponse xmlns:customns="http://test/">
         <return>Hello Catchwa!</return>
      </customns:helloResponse>
   </S:Body>
</S:Envelope>

我发现了一个类似于我所尝试做的事情的实现这里,但我在尝试执行类似代码时遇到了问题。(我想坚持使用Metro而不是改用cxf或axis)


执行

我实现的JAXBContextFactory返回一个JAXBRIContext看起来像这样:

import com.sun.xml.bind.api.JAXBRIContext;
import com.sun.xml.bind.api.TypeReference;
import com.sun.xml.ws.api.model.SEIModel;
import com.sun.xml.ws.developer.JAXBContextFactory;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

public class HelloJaxbContext implements JAXBContextFactory
{
  @Override
  public JAXBRIContext createJAXBContext(SEIModel seim, List<Class> classesToBind, List<TypeReference> typeReferences) throws JAXBException {
    List<Class> classList = new ArrayList<Class>();
    classList.addAll(classesToBind);

    List<TypeReference> refList = new ArrayList<TypeReference>();
    for (TypeReference tr : typeReferences) {
        refList.add(new TypeReference(new QName(tr.tagName.getNamespaceURI(), tr.tagName.getLocalPart(), "customns"), tr.type, tr.annotations));
    }
    return JAXBRIContext.newInstance(classList.toArray(new Class[classList.size()]), refList, null, seim.getTargetNamespace(), false, null);
  }  
}

一些用于 Web 服务的测试代码可能非常简单:

import com.sun.xml.ws.developer.UsesJAXBContext;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "Hello")
@UsesJAXBContext(value = HelloJaxbContext.class)
public class Hello
{
  @WebMethod(operationName = "hello")
  public String hello(@WebParam(name = "name") String txt)
  {
    return "Hello " + txt + "!";
  }
}

问题

在Tomcat 7.0.32和Glassfish 3.1.2中,使用从Maven获取的jaxws-rt 2.2.7时,以上代码不会影响我的Web服务输出(命名空间前缀仍为“ns2”)。


你是在Tomcat中运行这个Web服务,还是在Java分发中使用Metro?它们有不同的实现,因此有不同的解决方案。 - Zagrev
我将其使用几乎默认的NetBeans配置部署到Tomcat。 - Catchwa
所以我的回答对你没有帮助。你需要查看Tomcat源代码才能获得正确的信息。 - Zagrev
3个回答

28

如果您使用wsimport从旧服务的WSDL开始,并生成了所有各种JAXB注释的请求和响应包装器类,那么在生成的包中,您应该会找到一个名为package-info.java的文件。

@javax.xml.bind.annotation.XmlSchema(namespace = "http://test/")
package com.example.test;

JAXB提供了一种机制,让您可以在@XmlSchema注释上建议前缀映射,因此您可以尝试修改package-info.java文件如下:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://test/",
   xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "customns", 
         namespaceURI="http://test/")
   }
)
package com.example.test;

并查看是否对生成的消息有任何影响。这样做的优点是纯JAXB规范(即不依赖于RI特定的自定义上下文工厂)。

如果需要重新运行wsimport,您可以通过向xjc传递-npa选项来防止它覆盖已修改的package-info(这告诉它不要生成一个package-info.java文件,而是将所有必要的namespace设置放在类级别的注释中)。如何运行wsimport取决于您的具体情况。

命令行:

wsimport -B-npa ....

Ant:

<wsimport wsdl="..." destdir="..." .... >
  <xjcarg value="-npa" />
</wsimport>

Maven:

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <goals>
        <goal>wsimport</goal>
      </goals>
      <configuration>
        <xjcArgs>
          <xjcArg>-npa</xjcArg>
        </xjcArgs>
      </configuration>
    </execution>
  </executions>
</plugin>

谢谢,这个可行!你知道当我执行清理操作时,是否有任何方法可以覆盖我的package-info.java修改,或者我应该将其单独存储,然后在wsimport mvn任务运行后再复制它吗? - Catchwa
对于使用XJC的人,有一个命名空间前缀插件。 - Michael-O

4
你要实现的推荐/标准方法是使用SOAPMessage Handler。它们类似于Java Web应用程序过滤器(理论上也适用于此处),因为它们用于实现职责链模式。例如,在你的情况下,可以这样做:
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;


public class SOAPBodyHandler implements SOAPHandler<SOAPMessageContext> {

static final String DESIRED_NS_PREFIX = "customns";
static final String DESIRED_NS_URI = "http://test/";
static final String UNWANTED_NS_PREFIX = "ns";

@Override
public Set<QName> getHeaders() {
   //do nothing
   return null;
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
    if ((boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) { //Check here that the message being intercepted is an outbound message from your service, otherwise ignore.
        try {
            SOAPEnvelope msg = context.getMessage().getSOAPPart().getEnvelope(); //get the SOAP Message envelope
            SOAPBody body = msg.getBody();
            body.removeNamespaceDeclaration(UNWANTED_NS_PREFIX);
            body.addNamespaceDeclaration(DESIRED_NS_PREFIX, DESIRED_NS_URI); 
        } catch (SOAPException ex) {
            Logger.getLogger(SOAPBodyHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return true; //indicates to the context to proceed with (normal)message processing
}

@Override
public boolean handleFault(SOAPMessageContext context) {
      //do nothing
   return null;
}

@Override
public void close(MessageContext context) {
      //do nothing

}

在您的服务实现Bean类声明中,添加

  @HandlerChain(file = "handler-chain.xml")

上面的注释是对实际允许您处理程序启动的配置文件的引用。配置文件大致如下:
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <javaee:handler-chain>
           <javaee:handler>
              <javaee:handler-class>your.handler.FQN.here</javaee:handler-class>
           </javaee:handler>
        </javaee:handler-chain>
     </javaee:handler-chains> 

在家里尝试一下。这个特定的代码还没有经过测试。


我已经尝试了您的代码,但它更改的是SOAP响应中Body元素的命名空间和前缀,而不是JAX-WS生成的内容。也就是说,您的代码向Body元素添加了命名空间声明,但并未更改helloResponse元素的命名空间声明。感谢您的尝试! - Catchwa
我已成功使用消息处理程序来自定义命名空间前缀,详细概述在这里:https://dev59.com/dW865IYBdhLWcg3wYNfj#43316912 - bluecarbon

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接