java - How do I serialize a JAXBElement? -


i using:

  • gson 2.3.1
  • apache-cxf-3.1.1
  • jboss eap 6.4

i using wsdl2java create webservice client wsdl.

i generate classes with:

@xmlelementref(name = "fmla_code", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class) protected jaxbelement<string> fmlacode; 

when getting object webservice call want make json response caller

employmentinformationgetresponse response =             port.getemploymentinformationbyemployeeidentifier(employeenumberidentifier); gsonbuilder gsonbuilder = new gsonbuilder(); gsonbuilder.registertypehierarchyadapter(employmentinformationgetresponse.class, new employmentinformationgetresponseadapter()); gson gson = gsonbuilder.disablehtmlescaping().create(); string s = gson.tojson(response); system.out.println(s); 

the result contains

fmlacode=javax.xml.bind.jaxbelement@3191394e 

what best way me value?

i can prefer not alter generated classes, might need generated again later.

i tried create type adapter jaxbelement, not called:

public class jaxbelementadapter extends typeadapter<jaxbelement> {      @override     public void write(jsonwriter jsonwriter, jaxbelement jaxbelement) throws ioexception {         if (jaxbelement == null) {             jsonwriter.nullvalue();             return;         }         jaxbelement e = jaxbelement;         jsonwriter.value(e.getvalue() != null ? e.getvalue().tostring() : "");     }      @override     public jaxbelement read(jsonreader jsonreader) throws ioexception {         return null;     } } 

i used code register adapter:

    gsonbuilder.registertypehierarchyadapter(jaxbelement.class, new jaxbelementadapter()); 

here generated employmentinformationgetresponse

import javax.xml.bind.jaxbelement; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelementref; import javax.xml.bind.annotation.xmltype; import java.util.list;  /**  * <p>java class employmentinformationgetresponse complex type.  *   * <p>the following schema fragment specifies expected content contained within class.  *   * <pre>  * &lt;complextype name="employmentinformationgetresponse"&gt;  *   &lt;complexcontent&gt;  *     &lt;extension base="{http://www.ultipro.com/contracts}getresponse"&gt;  *       &lt;sequence&gt;  *         &lt;element name="results" type="{http://www.ultipro.com/contracts}arrayofemploymentinformation" minoccurs="0"/&gt;  *       &lt;/sequence&gt;  *     &lt;/extension&gt;  *   &lt;/complexcontent&gt;  * &lt;/complextype&gt;  * </pre>  *   *   */ @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "employmentinformationgetresponse", proporder = {     "results" }) public class employmentinformationgetresponse     extends getresponse {      @xmlelementref(name = "results", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<arrayofemploymentinformation> results;      /**      * gets value of results property.      *       * @return      *     possible object      *     {@link jaxbelement }{@code <}{@link arrayofemploymentinformation }{@code >}      *           */     public jaxbelement<arrayofemploymentinformation> getresults() {         return results;     }      /**      * sets value of results property.      *       * @param value      *     allowed object      *     {@link jaxbelement }{@code <}{@link arrayofemploymentinformation }{@code >}      *           */     public void setresults(jaxbelement<arrayofemploymentinformation> value) {         this.results = value;     } 

and employmentinformation without getters , setters

/**  *   */ @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "employmentinformation", proporder = {     "arrearssuspendedfrom",     "arrearssuspendedto",     "beneseniority",     "deceased",     "deceaseddate",     "earlyretirement",     "employeeidentifier",     "employmentstatus",     "fmlacode",     "hcsoenddate",     "hcsonotcovered",     "hcsostartdate",     "job",     "jobstart",     "lasthire",     "lastperfreview",     "lastsalaryreview",     "leavereason",     "nextperfreview",     "nextsalaryreview",     "originalhire",     "ptosuspendedfrom",     "ptosuspendedto",     "payautomatically",     "paysuspendedfrom",     "paysuspendedto",     "roeissuereason",     "regularretirement",     "selfserviceproperties",     "seniority",     "statusanticipatedend",     "statusstartdate",     "weeks" }) public class employmentinformation {      @xmlelement(name = "arrearssuspendedfrom")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar arrearssuspendedfrom;     @xmlelement(name = "arrearssuspendedto")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar arrearssuspendedto;     @xmlelement(name = "beneseniority")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar beneseniority;     @xmlelement(name = "deceased")     protected boolean deceased;     @xmlelement(name = "deceaseddate")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar deceaseddate;     @xmlelement(name = "earlyretirement")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar earlyretirement;     @xmlelementref(name = "employeeidentifier", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<employeeidentifier> employeeidentifier;     @xmlelementref(name = "employmentstatus", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<string> employmentstatus;     @xmlelementref(name = "fmla_code", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<string> fmlacode;     @xmlelement(name = "hcsoenddate")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar hcsoenddate;     @xmlelement(name = "hcsonotcovered")     protected boolean hcsonotcovered;     @xmlelement(name = "hcsostartdate")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar hcsostartdate;     @xmlelementref(name = "job", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<string> job;     @xmlelement(name = "jobstart")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar jobstart;     @xmlelement(name = "lasthire")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar lasthire;     @xmlelement(name = "lastperfreview")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar lastperfreview;     @xmlelement(name = "lastsalaryreview")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar lastsalaryreview;     @xmlelementref(name = "leavereason", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<string> leavereason;     @xmlelement(name = "nextperfreview")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar nextperfreview;     @xmlelement(name = "nextsalaryreview")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar nextsalaryreview;     @xmlelement(name = "originalhire")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar originalhire;     @xmlelement(name = "ptosuspendedfrom")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar ptosuspendedfrom;     @xmlelement(name = "ptosuspendedto")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar ptosuspendedto;     @xmlelement(name = "payautomatically")     protected boolean payautomatically;     @xmlelement(name = "paysuspendedfrom")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar paysuspendedfrom;     @xmlelement(name = "paysuspendedto")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar paysuspendedto;     @xmlelementref(name = "roeissuereason", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<string> roeissuereason;     @xmlelement(name = "regularretirement")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar regularretirement;     @xmlelementref(name = "selfserviceproperties", namespace = "http://www.ultipro.com/contracts", type = jaxbelement.class)     protected jaxbelement<arrayofkeyvalueofstringstring> selfserviceproperties;     @xmlelement(name = "seniority")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar seniority;     @xmlelement(name = "statusanticipatedend")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar statusanticipatedend;     @xmlelement(name = "statusstartdate")     @xmlschematype(name = "datetime")     protected xmlgregoriancalendar statusstartdate;     @xmlelement(name = "weeks")     protected double weeks;   } 

i'm not sure why code isn't working. think problem jaxbelement parameterized type, need specify generic type parameter.

try instead when registering typeadapter:

gsonbuilder.registertypeadapter(        new typetoken<jaxbelement<string>>(){}.gettype(),        new jaxbelementadapter()); 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -