Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: JAX-WS Mapping

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?

like image 815
mainstringargs Avatar asked Aug 30 '11 20:08

mainstringargs


People also ask

Which is the data mapping model of JAX-WS?

The JAX-WS specification describes the mapping between Web Services Description Language (WSDL) files and the Java language. The supported mappings include WSDL-to-Java mappings and Java-to-WSDL mappings. WSDL 1.1 is required by the JAX-WS 2.0 specification.

What is JAX-WS used for?

JAX-WS is a fundamental technology for developing SOAP (Simple Object Access Protocol) and RESTful (Web services that use representational state transfer, or REST, tools) Java Web services, where JAX-WS is designed to take the place of the JAVA-RPC (Remote Procedure Call) interface in Web services and Web-based ...

What is @WebService annotation in Java?

The @WebService annotation marks a Java class as implementing a Web service or marks a service endpoint interface (SEI) as implementing a web service interface. Important: A Java class that implements a web service must specify either the @WebService or @WebServiceProvider annotation.

What is the use of @WebService annotation?

The annotation @WebService tells the server runtime environment to expose all public methods on that bean as a web service. You can control more levels of granularity by adding other annotations on individual methods or parameters. Using annotations makes it much easier to expose Java artifacts as web services.


1 Answers

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

like image 194
Eran Medan Avatar answered Oct 09 '22 12:10

Eran Medan