Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

java - JAX-WS and Joda-Time?

How do I write a JAX-WS service so the @WebParam of my @WebMethod is a Joda-Time class like DateTime? Will @XmlTypeAdapter on a parameter work? I'm deploying to GlassFish 2.1.

Let me clarify the question because both answers so far have focused on binding custom types to existing JAXB classes, which is related but not the question I'm asking. How do I make the following @WebService accept joda DateTime objects as parameters?

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.joda.time.DateTime;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Resender {
    @WebMethod
    void resend(
            @WebParam(name = "start") DateTime start,
            @WebParam(name = "end") DateTime end
    );

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First write simple converter (to Calendar in this example, but can be easily changed to Joda-Time):

public class XsdDateTimeConverter {

    public static Calendar unmarshal(String dateTime) {
        final GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(DatatypeConverter.parseDate(dateTime).getTime());
        return calendar;
    }

    public static String marshal(Calendar calendar) {
        return DatatypeConverter.printDate(calendar);
    }

}

Next you have to introduce your converter to JAXB (xjb file):

<globalBindings>

    <javaType
            name="java.util.Calendar"
            xmlType="xs:dateTime"
            parseMethod="XsdDateTimeConverter.unmarshal"
            printMethod="XsdDateTimeConverter.marshal"
            />
    <javaType
            name="java.util.Calendar"
            xmlType="xs:date"
            parseMethod="XsdDateTimeConverter.unmarshal"
            printMethod="XsdDateTimeConverter.marshal"
            />
</globalBindings>

In the generated JAXB models xjc produced the following annotation:

@XmlJavaTypeAdapter(Adapter2.class)
@XmlSchemaType(name = "date")
protected Calendar date;

Where Adapter2.class is a generated adapter that wraps your POJO converter. As you can see Calendar is used instead of clumsy javax.xml.datatype.XMLGregorianCalendar. If you adjust this example to Joda-Time, please share it with us.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...