简体   繁体   中英

Java Soap Services: Add non-persistent result field based on a persistent one

So I need to add a new field to a SOAP service response. The thing is that the field has to take the value from a persistent field. I cannot add that persistent field directly. The persistent field returns a "Calendar" instance, which is, in fact, a DATETIME from MySQL. The current object uses the XmlAdapter.

I did something like this:

class SomeClassImpl extends SomeClass
{
    @Transient
    @XmlSchemaType(name="someDate")
    private String someDate;

    ...

    public void defSomeDate()
    {
        this.someDate = this.getPersistentDate().toString();
    }

    public String retSomeDate()
    {
        return this.someDate();
    }
}

The new field appears in the soap result, but the value is an exception, which I don't remember right now and I am not able to reproduce it now.

How would you do this? Is it possible to annotate a method instead of the member so it appears in the SOAP result? If yes, how would an annootation would look like?

Thank you in advance!

The problem was the following piece of code:

@XmlSchemaType(name="someDate")

There "name" parameter should be one of the standard data types for xml. In this case, because it contains the date and the time, it should be ' dateTime '. It could also be a string , but declaring it as dateTime makes the field more restrictive. Therefore, the correct annotation is:

@XmlSchemaType(name="dateTime")

With the date and time in mind, the second observation is that private String someDate; should be private CalendarsomeDate; , to be consistent and also for the actual code to work.

Annotating the methods is not required. Simply annotating the member/property is enough and as long as the member/property is set somewhere at runtime.

I hope this would be helpful for someone else too. It took me few hours to get this, but now I know how to proceed in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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