简体   繁体   English

Java:JAX-WS映射

[英]Java: JAX-WS Mapping

I am using JAX-WS for web services. 我正在将JAX-WS用于Web服务。

Whenever I use a char as a method parameter, I am getting it as an unsignedShort in the xsd (Focus on weatherLetter). 每当我将char用作方法参数时,我都会在xsd中将其作为unsignedShort来获取(着重于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? 我怎样才能将weatherLetter生成为Char或1个字母字符串之类的东西?

Update: 更新:

One way to do this is the in XSD (if you do contract first ) eg add an XSD Restriction to it directly, eg 一种实现方法是在XSD中(如果您先签约 ),例如直接向其添加XSD限制 ,例如

<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 (eg write code that generates the XSD, not vice versa) 但是我认为问题是合同最后的问题(例如,编写生成XSD的代码,反之亦然)

This is not yet supported in JAX-WS or JAXB, as far as I know (but a nice enhancement request) 据我所知,JAX-WS或JAXB尚不支持此功能(但有一个不错的增强要求)

Sources: 资料来源:

JAX-WS and JAXB don't have support for code generation for restrictions from XSD (eg xsd:restriction) http://old.nabble.com/Does-jaxb-2.1-enforce-xs:restriction-td21348458.html JAX-WS和JAXB不支持针对XSD的限制生成代码(例如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 看一下这个: 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? JAX-WS和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. 我认为您不能通过其他任何方式将其限制为1个字符,一个字符确实是一个无符号的short,并且您可以将其限制为1个“字符串”字符。

If you use a String, you will allow unlimited chars. 如果使用字符串,则将允许无限字符。 and @WebParam doesn't have an API to limit length @WebParam没有API来限制长度

Actually I didn't see a way to do XSD restrictions at all using JAX-WS, but I may be wrong 实际上,我完全没有看到使用JAX-WS进行XSD限制的方法,但是我可能错了

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. char和java.lang.Character要求您输入自定义映射,因为从char或java.lang.Character到WSDL XSD的默认映射不存在。

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

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): 然后在您的WXStatus中(将单个类定义为输入参数,而不是传递单个参数的负载-JAX已经将其转换为complexType,因此您也可以加上一种更好的OOP样式),添加此批注(字段或获取方法):

@XmlJavaTypeAdapter(CharAdapter.class)
char weatherLetter;

This will allow un/marshalling on your server-side and the client will see it as an xs:string. 这将允许在服务器端取消/编组,并且客户端会将其视为xs:string。 One side effect is as we're using primitive wrapper for char, you'll have to handle null. 副作用是,因为我们对char使用原始包装器,因此必须处理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: 更新编辑:我不认为有任何方法可以用此方法指定字符串的长度,而无需手动创建/编辑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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM