Java:JAX-WS 映射

发布于 2024-12-02 13:38:45 字数 1591 浏览 1 评论 0原文

我正在使用 JAX-WS 进行 Web 服务。

每当我使用 char 作为方法参数时,我都会将其作为 xsd 中的 unsignedShort 获取(重点关注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
) 

这是我得到的类型映射:

<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 个字母字符串或其他内容?

I am using JAX-WS for web services.

Whenever I use a char as a method parameter, I am getting it as an unsignedShort in the xsd (Focus on weatherLetter).

Here is the declaration:

@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>

How can I get weatherLetter to generate as a Char or 1 Letter String or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-12-09 13:38:45

更新:

一种方法是在 XSD 中(如果您首先签订合同),例如添加 XSD 限制 直接对其进行,例如

<xs:element name="singleChar">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="1"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

但我认为问题在于 contract last (例如编写生成 XSD 的代码,而不是反之亦然)

这是据我所知,JAX-WS 或 JAXB 尚不支持(但这是一个很好的增强请求)

来源:

JAX-WS 和 JAXB 不支持 XSD 限制的代码生成(例如xsd:限制)
http://old.nabble.com/Does- jaxb-2.1-enforce-xs:restriction-td21348458.html

原因是另一个方向(生成限制通过注释),也不支持

解决方法:

看看这个:http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

还有这个问题:

JAX-WS 和 Joda-Time?

没有完全按照您的方式行事想要,但让你更接近一点


我认为你不能以任何其他方式将其限制为 1 个字符,一个 char 确实是一个无符号短整型,并且最接近限制为 1 个“字符串”字符。

如果您使用字符串,则将允许无限个字符。并且 @WebParam 没有限制长度的 API

实际上我根本没有看到使用 JAX-WS 进行 XSD 限制的方法,但我可能是错的

Update:

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>

But I think the question is on contract last (e.g. write code that generates the XSD, not vice versa)

This is not yet supported in JAX-WS or JAXB, as far as I know (but a nice enhancement request)

Sources:

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

The reason is that the other direction (generating restrictions by Annotation), is not supported too

Workaround:

Take a look at this: http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

And also at this question:

JAX-WS and Joda-Time?

Not doing exactly what you want, but getting you a little bit closer


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.

If you use a String, you will allow unlimited chars. and @WebParam doesn't have an API to limit length

Actually I didn't see a way to do XSD restrictions at all using JAX-WS, but I may be wrong

扭转时空 2024-12-09 13:38:45

char 和 java.lang.Character 要求您输入自定义映射,因为从 char 或 java.lang.Character 到 WSDL XSD 的默认映射不存在。

引用自 http://publib.boulder.ibm.com/infocenter/radhelp/v6r0m1/index.jsp?topic=%2Fcom.ibm.etools.webservice.creation.doc%2Fconcepts%2Fcsoaptover.html

char and java.lang.Character require you to enter custom mappings since default mappings from char or java.lang.Character to WSDL XSD do not exist.

Quoted from http://publib.boulder.ibm.com/infocenter/radhelp/v6r0m1/index.jsp?topic=%2Fcom.ibm.etools.webservice.creation.doc%2Fconcepts%2Fcsoaptover.html

随波逐流 2024-12-09 13:38:45

使用适配器,例如:

import javax.xml.bind.annotation.adapters.XmlAdapter;


public class CharAdapter extends XmlAdapter<String, Character> {

    @Override
    public String marshal(Character c) throws Exception
    {
        return String.valueOf(c);
    }

    @Override
    public Character unmarshal(String s) throws Exception
    {
    if(s == null) {
        return null;
    }
    if(s.length() != 1) {
        throw new IllegalArgumentException("Provided string \"" + s + 
                "\" has invalid length of " + s.length() + " should be 1");
    }
        return s.charAt(0);
    }

}

然后在您的 WXStatus 中(定义单个类作为输入参数,而不是传递大量单独的参数 - JAX 已经将其转换为复杂类型,所以您也可以,而且它是更好的 OOP 风格),添加此注释(到字段或 getter):

@XmlJavaTypeAdapter(CharAdapter.class)
char weatherLetter;

这将允许在服务器端进行取消/编组,并且客户端会将其视为 xs:string。一个副作用是,当我们使用 char 的原始包装器时,您必须处理 null。

更新编辑:我认为没有任何方法可以用它来指定字符串的长度,而无需使用以下内容手动创建/编辑 WSDL:

<xs:simpleType name="weatherLetter">
  <xs:annotation>
    <xs:documentation>weather character info blah blah</xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:string">
    <xs:length value="1"/>
  </xs:restriction>
</xs:simpleType>

Use an adapter, something like:

import javax.xml.bind.annotation.adapters.XmlAdapter;


public class CharAdapter extends XmlAdapter<String, Character> {

    @Override
    public String marshal(Character c) throws Exception
    {
        return String.valueOf(c);
    }

    @Override
    public Character unmarshal(String s) throws Exception
    {
    if(s == null) {
        return null;
    }
    if(s.length() != 1) {
        throw new IllegalArgumentException("Provided string \"" + s + 
                "\" has invalid length of " + s.length() + " should be 1");
    }
        return s.charAt(0);
    }

}

And then in your WXStatus (define a single class as the input argument instead of passing in loads of individual parameters - JAX is already turning this into a complexType so you may as well, plus it's a better OOP style), add this annotation (to either field or getter):

@XmlJavaTypeAdapter(CharAdapter.class)
char weatherLetter;

This will allow un/marshalling on your server-side and the client will see it as an xs:string. One side effect is as we're using primitive wrapper for char, you'll have to handle null.

UPDATE EDIT: I don't think there's any way you can specify the length of the string with this though, without manually creating/editing your WSDL with something like:

<xs:simpleType name="weatherLetter">
  <xs:annotation>
    <xs:documentation>weather character info blah blah</xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:string">
    <xs:length value="1"/>
  </xs:restriction>
</xs:simpleType>
森末i 2024-12-09 13:38:45

如果您只接收一个字符,您应该尝试在声明中将其从 char 更改为 String,如果您正在对客户端进行编程,那么这样做应该不会有太大麻烦

You should try changing it from char to String in your declaration if you are to receive just a single character, if you're programming the client side there shouldn't be much trouble in doing so

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文