gpt4 book ai didi

java - 如何序列化 JAXBElement?

转载 作者:行者123 更新时间:2023-12-02 04:47:17 26 4
gpt4 key购买 nike

我在用:

  • GSON 2.3.1
  • apache-cxf-3.1.1
  • JBoss EAP 6.4

  • 我正在使用 wsdl2java 从 wsdl 创建 webservice 客户端。

    我用以下方法生成类:
    @XmlElementRef(name = "FMLA_Code", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
    protected JAXBElement<String> fmlaCode;

    从 webservice 调用中取回对象时,我想对调用者进行 JSON 响应
    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);

    结果包含
    fmlaCode=javax.xml.bind.JAXBElement@3191394e

    我获得值(value)的最佳方式是什么?

    我可以但不希望更改生成的类,因为它们可能需要稍后再次生成。

    我试图为 JAXBElement 创建一个类型适配器,但它不会被调用:
    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;
    }
    }

    我使用此代码来注册适配器:
        gsonBuilder.registerTypeHierarchyAdapter(JAXBElement.class, new JAXBElementAdapter());

    这是生成的 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 for EmploymentInformationGetResponse complex type.
    *
    * <p>The following schema fragment specifies the expected content contained within this 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 the value of the results property.
    *
    * @return
    * possible object is
    * {@link JAXBElement }{@code <}{@link ArrayOfEmploymentInformation }{@code >}
    *
    */
    public JAXBElement<ArrayOfEmploymentInformation> getResults() {
    return results;
    }

    /**
    * Sets the value of the results property.
    *
    * @param value
    * allowed object is
    * {@link JAXBElement }{@code <}{@link ArrayOfEmploymentInformation }{@code >}
    *
    */
    public void setResults(JAXBElement<ArrayOfEmploymentInformation> value) {
    this.results = value;
    }

    以及没有 getter 和 setter 的 EmploymentInformation
    /**
    *
    */
    @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;


    }

    最佳答案

    我不确定为什么您的代码不起作用。我认为最可能的问题是JAXBElement是参数化类型,因此您需要使用泛型类型参数指定它。

    注册您的 TypeAdapter 时试试这个:

    gsonBuilder.registerTypeAdapter(
    new TypeToken<JAXBElement<String>>(){}.getType(),
    new JAXBElementAdapter());

    关于java - 如何序列化 JAXBElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886121/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com