使用Metro编组JAXB类时忽略schemaLocation [英] schemaLocation ignored when marshalling JAXB Classes using Metro

查看:182
本文介绍了使用Metro编组JAXB类时忽略schemaLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用捆绑了Java 6 SE的Metro堆栈来调用Web服务。 Web服务期望XML作为参数。我使用JAXB类来创建内容并将我的JAXB根元素传递给Web服务端点。

I'm using the Metro stack bundled with Java 6 SE to call a web service. The web service expects XML as a parameter. I use JAXB classes to create content and pass my JAXB root element to the web service endpoint.

现在我的问题:我找不到任何方法来编写marshaller包括XSD文件的schemaLocation,因为我无法直接访问marshaller。 (如果您可以直接访问marshaller,可以将schemalocation设置为属性,但在使用metro时,所有编组似乎都在内部进行。)

Now to my Problem: I can't find any way to make the marshaller include the schemaLocation of the XSD file since I can't directly access the marshaller. (If you have direct access to the marshaller it is possible do set the schemalocation as a property, but when using metro, all the marshalling seems to be happening internally.)

我已经尝试在生成的package-info.java类中的XmlSchema注释中设置xsi:schemaLocation,但这对生成的xml没有影响。

I've tried setting the xsi:schemaLocation in the XmlSchema annotation in the generated package-info.java class, but this had no effect on the xml generated.

另一点是在创建Web服务客户端并在Java SE环境中调用Web服务时,某些注释(如@ UsesJAXBContext,@ WebServiceClient和@XmlSchema)似乎被忽略。 (我必须在此声明我是Java Web服务的初学者)

Another point is that when creating a web service client and calling a web service in an Java SE environment, certain annotations like @UsesJAXBContext, @WebServiceClient and @XmlSchema seem to be ignored. (I must state here that I am a beginner in terms of Java web services)

推荐答案

好的,这就是我现在所知道的。几个月来,这对我来说一直是个问题。

Ok, here's what I now know. This has been a problem for me for months.

首先,您必须更改JAX-WS使用的JAXBContext。为此,请在服务器上使用@UsesJAXBContext注释。 (com.sun.xml.ws.developer.UsesJAXBContext)

First, you have to change the JAXBContext used by JAX-WS. To do this use the @UsesJAXBContext annotation on the server. (com.sun.xml.ws.developer.UsesJAXBContext)

然后,在工厂实现中,您必须使用此方法返回自定义桥。

Then, in your factory implementation, you have to return custom Bridges in this method.

public Bridge createBridge(final TypeReference typereference)

然后你的自定义网桥需要设置marshaller属性来设置你想要使用的命名空间映射器。

Then your custom bridge needs to set the marshaller property to set the namespace mapper you want to use.

这是我的例子。

@WebService(serviceName = ...)
@UsesJAXBContext(MyContextFactory.class)
public class SoapServer { ... }

和工厂类......

and the factory class ...

public static class MyContextFactory implements JAXBContextFactory
{
    @Override
    public JAXBRIContext createJAXBContext(final SEIModel sei,
            @SuppressWarnings("rawtypes") final List<Class> classesToBind, final List<TypeReference> typeReferences)
            throws JAXBException
    {
        JAXBRIContext context = JAXBContextFactory.DEFAULT.createJAXBContext(sei, classesToBind, typeReferences);
        return new MyJaxwsContext(context);
    }
}

以及JAXB Context forlelelement ......

and the JAXB Context impelementation...

public class MyContext extends JAXBRIContext
{
/** the actual context */
private final JAXBRIContext delegate;

public MyContext(final JAXBRIContext createContext)
{
    this.delegate = createContext;
}

public Bridge createBridge(final TypeReference arg0)
{
    return new MyBridge((JAXBContextImpl) delegate, delegate.createBridge(arg0));
}

现在是Bridge实施......

and now the Bridge implementation...

public class MyBridge extends Bridge
{
private final Bridge delegate;

protected MyBridge(final JAXBContextImpl context, final Bridge delegate)
{
    super(context);
    this.delegate = delegate;
}

// an example marshal call. There are many more...
public void marshal(final Marshaller m, final Object object, final ContentHandler contentHandler)
        throws JAXBException
{
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", namespaceMapper);
    delegate.marshal(m, object, contentHandler);
}

注意:我刚刚包装了现有的实施方案。我想要的就是能够修复命名空间名称。从我阅读源代码(JAXWS),这是到达编组人员的唯一方法。

NOTE: I have just wrapped the existing implementation. All I wanted was to be able to fix the namespace names. From my reading of the source (JAXWS), this is the only way to get to the marshaller.

NOTE2 有一个向下转向RI决赛课。这仅适用于参考实现。 YMMV

NOTE2 There is a downcast to an RI final class. This only works with the reference implementation. YMMV

这篇关于使用Metro编组JAXB类时忽略schemaLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆