Java:JAX-WS 映射 [英] Java: JAX-WS Mapping
问题描述
我将 JAX-WS 用于 Web 服务.
I am using JAX-WS for web services.
每当我使用 char 作为方法参数时,我都会将它作为 xsd 中的 unsignedShort 获取(关注weatherLetter).
Whenever I use a char as a method parameter, I am getting it as an unsignedShort in the xsd (Focus on weatherLetter).
声明如下:
@WebMethod
public boolean setWXtatus(
@WebParam(name = "weatherLetter") char weatherLetter,
@WebParam(name = "weatherDigit") int weatherDigit,
@WebParam(name = "cloudCover") int cloudCover,
@WebParam(name = "cloudBaseInHundredsOfFeet") int cloudBaseInHundredsOfFeet,
@WebParam(name = "pressureInHg") int pressureInHg,
@WebParam(name = "visibilityInKm") int visibilityInKm,
@WebParam(name = "windSpeed") int windSpeed,
@WebParam(name = "windDirection") int windDirection,
@WebParam(name = "lastUpdateHour") int lastUpdateHour,
@WebParam(name = "lastUpdateMin") int lastUpdateMin
)
这是我得到的类型映射:
Here is the type mappings I get:
<xs:complexType name="setWXStatus">
<xs:sequence>
<xs:element name="weatherLetter" type="xs:unsignedShort" minOccurs="0"/>
<xs:element name="weatherDigit" type="xs:int"/>
<xs:element name="cloudCover" type="xs:int"/>
<xs:element name="cloudBaseInHundredsOfFeet" type="xs:int"/>
<xs:element name="pressureInHg" type="xs:int"/>
<xs:element name="visibilityInKm" type="xs:int"/>
<xs:element name="windSpeed" type="xs:int"/>
<xs:element name="windDirection" type="xs:int"/>
<xs:element name="lastUpdateHour" type="xs:int"/>
<xs:element name="lastUpdateMin" type="xs:int"/>
</xs:sequence>
</xs:complexType>
如何让weatherLetter 生成为Char 或1 个字母字符串之类的?
How can I get weatherLetter to generate as a Char or 1 Letter String or something?
推荐答案
更新:
一种方法是在 XSD 中(如果您先签约),例如直接向其添加 XSD 限制,例如
One way to do this is the in XSD (if you do contract first) e.g. add an XSD Restriction to it directly, e.g.
<xs:element name="singleChar">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
但我认为问题在于合同最后(例如,编写生成 XSD 的代码,反之亦然)
But I think the question is on contract last (e.g. write code that generates the XSD, not vice versa)
据我所知,JAX-WS 或 JAXB 尚不支持此功能(但这是一个很好的增强请求)
This is not yet supported in JAX-WS or JAXB, as far as I know (but a nice enhancement request)
来源:
JAX-WS 和 JAXB 不支持针对来自 XSD 的限制(例如 xsd:restriction)的代码生成http://old.nabble.com/Does-jaxb-2.1-enforce-xs:restriction-td21348458.html
JAX-WS and JAXB don't have support for code generation for restrictions from XSD (e.g. xsd:restriction) http://old.nabble.com/Does-jaxb-2.1-enforce-xs:restriction-td21348458.html
原因是另一个方向(通过Annotation产生限制),也不支持
The reason is that the other direction (generating restrictions by Annotation), is not supported too
解决方法:
还有这个问题:
不是做你想做的,而是让你更接近一点
Not doing exactly what you want, but getting you a little bit closer
我认为您不能以任何其他方式将其限制为 1 个字符,一个字符确实是一个无符号短整型,并且您可以将其限制为 1 个字符串"字符.
I don't think you can have it limited to 1 char any other way, a char is indeed an unsigned short, and the closest thing you can to limiting to 1 "String" char.
如果您使用字符串,您将允许无限字符.并且 @WebParam 没有限制长度的API
If you use a String, you will allow unlimited chars. and @WebParam doesn't have an API to limit length
实际上我根本没有看到使用 JAX-WS 进行 XSD 限制的方法,但我可能错了
Actually I didn't see a way to do XSD restrictions at all using JAX-WS, but I may be wrong
这篇关于Java:JAX-WS 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!