jaxb etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
jaxb etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

4 Nisan 2011 Pazartesi

problem passing time info through jaxws web service

reading http://hilaltarakci.blogspot.com/2009/10/revisited-troobleshooting-date-becomes.html beforehand may be useful for this post..


there was a problem while passing date info through jaxws web service; time info was absent..
However, editing the DateAdapter as follows solved the problem:

public class DateAdapter {

public static Date parseDate(String s) {
System.out.println("========= DateAdapter.parseDate param:" + s);
System.out.println("========= DateAdapter.parseDate:" + DatatypeConverter.parseDate(s).getTime().toString());
return DatatypeConverter.parseDate(s).getTime();
}

public static String printDate(Date dt) {
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
//System.out.println("========= DateAdapter.printDate param:" + dt.toString());
System.out.println("========= DateAdapter.printDate: " + DatatypeConverter.printDateTime(cal).toString());
return DatatypeConverter.printDateTime(cal).toString();
}
}

27 Ekim 2009 Salı

revisited : Troobleshooting: Date becomes XMLGregorianCalendar when stubs are generated at client side, how to prevent this?

lets revisit issue on http://hilaltarakci.blogspot.com/2009/06/troobleshooting-date-becomes.html
the problem is, although adapter interface is generated, the generated implementation for the interface is empty.. so, sould replace with own implementation..

btw, soln is discovered by someone in the team, not me.. lets share it :  https://jaxb.dev.java.net/guide/Using_different_datatypes.html


19 Ağustos 2009 Çarşamba

converting java classes to schema: schemagen

schemagen tool comes with jaxb reference implementation and can be used to generate schemas from java classes. here is the command line version http://java.sun.com/webservices/docs/2.0/jaxb/schemagen.html and ant version https://jaxb.dev.java.net/jaxb20-ea/docs/schemagenTask.html When command line version is used, the names of the generated schema files are given automatically as schema1.xsd, schema2.xsd,.. etc. (one schema file per namespace). However, controlling names of files is possible when ant task is used, so i prefer using ant task.
Here is my sample schemagen ant file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="projectname" default="generate-schema" basedir=".">

<path id="classpath">
<pathelement location="path/to/classes"/>
<fileset dir="/path/to/jaxb/lib">
<include name="**/*.jar"/>
</fileset>
</path>

<taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask">
<classpath refid="classpath"/>
</taskdef>

<target name="generate-schema">
<schemagen srcdir="path/to/src" destdir="path/to/generatedfiles">
<classpath refid="classpath"/>
<schema namespace="http://namespace" file="nameOfMySchema.xsd" />
</schemagen>
</target>

</project>

However, if java classes contains jpa specific annotations, the apt tool could not handle this and throw annotation specific exceptions when the lib coming with jaxb ri is used in the classpath.
Annotations for Persistence part on http://www.devx.com/Java/Article/34069/0/page/1 solves this. download sample code on http://assets.devx.com/sourcecode/18778.zip and use the lib coming with the sample on path 18778/JAXB/lib. Actually, the above ant file is a simplified version of 18778/JAXB/3-JAXB and JPA/build.xml coming with the example..

So, it is ok for now :)

19 Haziran 2009 Cuma

Troobleshooting: Date becomes XMLGregorianCalendar when stubs are generated at client side, how to prevent this?

The Problem Part:

The environment: Netbeans 6.5 Ide and Glassfish as, on x64 linux machine (open suse).

Here is the sample web service with java.util.Date type:

package com.example.date;

import java.util.Date;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

@WebService()

public class DateWebService {

@WebMethod(operationName = "dateOperation")

public Date dateOperation(@WebParam(name = "date")

Date date) {

return date;

}

}

The wsdl is at http://localhost:8080/DateProject/DateWebServiceService?WSDL

The dependent xsd is at http://localhost:8080/DateProject/DateWebServiceService?xsd=1

The input and output types for dateOperation is as follows:

<xs:complexType name="dateOperation">

<xs:sequence>

<xs:element name="date" type="xs:dateTime" minOccurs="0"/>

(<xs:sequence>

</xs:complexType>

<xs:complexType name="dateOperationResponse">

<xs:sequence>

<xs:element name="return" type="xs:dateTime" minOccurs="0"/>

</xs:sequence>

<xs:complexType>

It is obvious that java.util.Date is mapped to xs:dateTime.

Lets see what happens when we generate the client stubs from the wsdl. (This is done by right clicking the project in Netbeans and selecting New Web Service Client and entering the wsdl to the related input.)

The generated stubs are at the following location:



Java.util.Date becomes javax.xml.datatype.XMLGregorianCalendar when xml is mapped to Java types againL (Comments are omitted from the following generated code for simplicity..)

package com.example.date;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlSchemaType;

import javax.xml.bind.annotation.XmlType;

import javax.xml.datatype.XMLGregorianCalendar;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "dateOperation", propOrder = {

"date"

})

public class DateOperation {

@XmlSchemaType(name = "dateTime")

protected XMLGregorianCalendar date;

public XMLGregorianCalendar getDate() {

return date;

}


public void setDate(XMLGregorianCalendar value) {

this.date = value;

}

}

The reason for this is explained here: http://forums.java.net/jive/message.jspa?messageID=166006

So, the question is what sould be done to get java.util.Date instead of XMLGregorianCalendar when the stubs are generated at the client side?


The Solution Part:

Data conversion at the client side is a solution: http://www.velocityreviews.com/forums/t462336-convert-jaxb-xmlgregoriancalendar-to-javasqldate.html

However, this may not be the case if you definitely want to get Date back when stubs are generated..

Prepare the following file i named jax-ws-jaxb-customization.xml:


<?xml version="1.0" encoding="UTF-8"?>

<jaxws:bindings  node="wsdl:definitions/wsdl:types/xsd:schema"

                                xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"

                                xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"

                                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

                                xmlns:xsd="http://www.w3.org/2001/XMLSchema" wsdlLocation="../wsdl/localhost_8080/DateProject/DateWebServiceService.wsdl">

<jaxb:globalBindings>

                               <jaxb:serializable/>

                               <jaxb:javaType name="java.util.Date"

                                                      xmlType="xsd:dateTime"/>

                </jaxb:globalBindings>

</jaxws:bindings>

Use Netbeans Wsdl Customizer :

Right click the web service under Web Service References, seleck Edit Web Service Attributes and select the second tab named WSDLCustomization

Select the below External Binding File part and add jax-ws-jaxb-customization.xml as follows:

Now, examine the generated stubs again, DateOperation is now as follows (again comments are removed for simplicity):

package com.example.date;

import java.io.Serializable;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlSchemaType;

import javax.xml.bind.annotation.XmlType;

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

import org.w3._2001.xmlschema.Adapter1;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "dateOperation", propOrder = {

"date"

})

public class DateOperation

implements Serializable

{

@XmlElement(type = String.class)

@XmlJavaTypeAdapter(Adapter1 .class)

@XmlSchemaType(name = "dateTime")

protected Date date;

public Date getDate() {

return date;

}

public void setDate(Date value) {

this.date = value;

}

}

And now there is a difference in the generated stubs, an Adapter1 is also generated:

package com.example.date;

import java.io.Serializable;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlSchemaType;

import javax.xml.bind.annotation.XmlType;

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

import org.w3._2001.xmlschema.Adapter1;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "dateOperation", propOrder = {

"date"

})

public class DateOperation

implements Serializable

{

@XmlElement(type = String.class)

@XmlJavaTypeAdapter(Adapter1 .class)

@XmlSchemaType(name = "dateTime")

protected Date date;

public Date getDate() {

return date;

}

public void setDate(Date value) {

this.date = value;

}

}

So, problem solved for now :)

Edit on 22.06.2009  14.15 : The display problem in xml part of the post is fixed by now :)