本文介绍了使用XJC生成Java源时使用JAXWS enableWrapperStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从XSD生成Java源代码,并且必须使用JAXWS禁用包装器样式。我编写了自定义绑定,但似乎JAXWS不能与XJC一起使用。我使用的绑定非常简单。

I'm trying to generate Java source from XSD and have to disable wrapper style with JAXWS. I've written the custom binding but it seems that JAXWS doesn't work with XJC. The binding I use is pretty simple.

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns="http://java.sun.com/xml/ns/jaxws"
version="2.1" jaxb:extensionBindingPrefixes="xjc">

<jaxb:bindings>
    <jaxb:globalBindings typesafeEnumMaxMembers="2000" generateElementProperty="false" >
         <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    </jaxb:globalBindings>
</jaxb:bindings>

如果我正在尝试运行ant脚本我收到以下错误消息。

If I'm trying to run the ant script I'm getting following error messages.

 [xjc] [ERROR] Unsupported binding namespace "http://java.sun.com/xml/ns/jaxws". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
  [xjc]   line 2 of file:/D:/xxxxxxxxxx/xxxxx/xxxx.xsd
  [xjc] [ERROR] cvc-complex-type.2.4.a: Invalid content was found starting with element 'jaxws:enableWrapperStyle'. One of '{"http://java.sun.com/xml/ns/jaxb":javaType, "http://java.sun.com/xml/ns/jaxb":serializable, "http://java.sun.com/xml/ns/jaxb/xjc":serializable, "http://java.sun.com/xml/ns/jaxb/xjc":superClass, "http://java.sun.com/xml/ns/jaxb/xjc":superInterface, "http://java.sun.com/xml/ns/jaxb/xjc":typeSubstitution, "http://java.sun.com/xml/ns/jaxb/xjc":smartWildcardDefaultBinding, "http://java.sun.com/xml/ns/jaxb/xjc":simple, "http://java.sun.com/xml/ns/jaxb/xjc":treatRestrictionLikeNewType, "http://java.sun.com/xml/ns/jaxb/xjc":javaType, "http://java.sun.com/xml/ns/jaxb/xjc":generateElementProperty, "http://java.sun.com/xml/ns/jaxb/xjc":noMarshaller, "http://java.sun.com/xml/ns/jaxb/xjc":noUnmarshaller, "http://java.sun.com/xml/ns/jaxb/xjc":noValidator, "http://java.sun.com/xml/ns/jaxb/xjc":noValidatingUnmarshaller}' is expected.
  [xjc]   line 8 of file:/D:/xxxxxxxxxx/xxxxx/xxxx/xsd/xsd-binding.xml

我已经尝试过只使用jaxws,但XJC将JAXB视为主绑定。
使用此绑定:

I have already tryed to use only jaxws, but XJC extpects JAXB as main binding.With this binding:

<bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://java.sun.com/xml/ns/jaxws"
    jaxb:version="2.1" extensionBindingPrefixes="xjc annox">
    <enableWrapperStyle>false</enableWrapperStyle>
    <jaxb:bindings>
        <jaxb:globalBindings typesafeEnumMaxMembers="2000" generateElementProperty="false" />
    </jaxb:bindings>
</bindings>

我收到错误:

[xjc] [ERROR] not an external binding file. The root element must be {http://java.sun.com/xml/ns/jaxb}bindings but it is {http://java.sun.com/xml/ns/jaxws}bindings
      [xjc]   line ? of file:/D:/xxxxxx/xsd-binding.xml
      [xjc] [ERROR] Unexpected <bindings> appears at line 4 column 61
      [xjc]   line 4 of file:/D:/xxxxxx/xsd-binding.xml

是否可以在jaxb中使用 jaxws:enableWrapperStyle ?如果是的话,我忽略了什么?提前谢谢!

Is it possible to use the jaxws:enableWrapperStyle inside of jaxb? If yes, what have I overlooked? Thank you in advance!

推荐答案

我找到了解决方案。 JAXWS元素必须在JAXB元素内部,并且必须声明如下:

I've found the solution. JAXWS-element has to be inside of JAXB-element and has to be declared as follows:

<?xml version="1.0"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1" jaxb:extensionBindingPrefixes="xjc annox">
    <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
        <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    </jaxws:bindings>
    <jaxb:bindings>
        <jaxb:globalBindings typesafeEnumMaxMembers="2000" generateElementProperty="false"/>
    </jaxb:bindings>
</jaxb:bindings>

这篇关于使用XJC生成Java源时使用JAXWS enableWrapperStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 19:41