【发布时间】:2012-10-05 02:11:57
【问题描述】:
目标
我正在为一个相当旧的(但遗憾的是不可更改的)界面实现 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>
我发现了类似于我正在尝试做的事情here,但我无法让类似的代码正确执行。 (我想坚持使用 Metro 而不必更改为 cxf 或轴)
执行
返回JAXBRIContext 的JAXBContextFactory 实现如下所示:
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 + "!";
}
}
问题
在使用 jaxws-rt 2.2.7(来自 Maven)的 Tomcat 7.0.32 和 Glassfish 3.1.2 中,上述代码不会影响我的 Web 服务输出(命名空间前缀仍然是“ns2”)。
【问题讨论】:
-
您是在 Tomcat 中运行此 Web 服务,还是在 Java 发行版中使用 Metro?它们有不同的实现,因此也有不同的解决方案。
-
我正在使用或多或少的默认 NetBeans 配置将其部署到 Tomcat。
-
所以我的回答对你没有帮助。您需要通过 Tomcat 源来获取正确的信息。