久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

@XmlElement 具有多個名稱

@XmlElement with multiple names(@XmlElement 具有多個名稱)
本文介紹了@XmlElement 具有多個名稱的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我這里有一個情況,試圖充當兩個 API 之間的網關.我需要做的是:

I have a situation here, trying to act as a gateway between two APIs. What I need to do, is:

  1. 向 APIa 發出請求;
  2. 將 XML 響應解析(編組)為 java 對象;
  3. 對其稍作改動;
  4. 然后以 XML(解組)形式向另一端 (APIb) 提供響應.

問題是我使用同一個對象來解析 API 響應并將響應發送到另一端.

The thing is that I use the same object to parse the API response and to send the response to the other end.

public class ResponseAPI{

@XmlElement(name="ResponseCode") //I receive <ResponseCode> but I need to send <ResultCode>
private String responseCode;

//getter and setter
}

正如評論所說:我收到但我需要發送

as the comment says: I receive but I need to send

有沒有辦法在不創建另一個帶有 ResultCode 的額外類的情況下完成這項工作?

Is there a way to get this done without having to create another extra class which carries ResultCode?

提前致謝!

推薦答案

注意:

Ilya 給出的答案有效,但不保證適用于所有 JAXB 實現,甚至適用于不同版本的 JAXB單個 JAXB 實現.當決定封送哪個元素取決于值的類型時,@XmlElements 注釋很有用(請參閱:http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html).在您的用例中,ResponseCodeResultCode 元素都對應于類型 String,解組將始終正常工作,但選擇要輸出的元素是任意的.一些 JAXB Impls 可能有最后指定的勝利,但其他人可能很容易獲得第一次勝利.

The answer given by Ilya works but isn't guaranteed to work across all implementations of JAXB or even across versions of a single JAXB implementation. The @XmlElements annotation is useful when the decision of which element to marshal depends on the type of the value (see: http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html). In your use case both the ResponseCode and ResultCode elements correspond to type String, unmarshalling will always work fine, but the choice of which element to output is arbitrary. Some JAXB Impls may have last specified wins, but others could easily have first wins.

您可以利用 @XmlElementRef 執行以下操作.

You could do the following by leveraging @XmlElementRef.

響應API

我們將 responseCode 屬性從 String 類型更改為 JAXBElement.JAXBElement 允許我們存儲元素名稱和值.

We will change the responseCode property from type String to JAXBElement<String>. The JAXBElement allows us to store the element name as well as the value.

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseAPI{

    @XmlElementRefs({
        @XmlElementRef(name = "ResponseCode"),
        @XmlElementRef(name = "ResultCode")
    })
    private JAXBElement<String> responseCode;

    public JAXBElement<String> getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(JAXBElement<String> responseCode) {
        this.responseCode = responseCode;
    }

}

對象工廠

我們在 ResponseAPI 類上使用的 @XmlElementRef 注釋對應于使用 @XmlRegistry<注釋的類上的 @XmlElementDecl 注釋/代碼>.傳統上,此類稱為 ObjectFactory,但您可以隨意調用它.

The @XmlElementRef annotations we used on the ResponseAPI class correspond to @XmlElementDecl annotations on a class annotated with @XmlRegistry. Traditionally this class is called ObjectFactory but you can call it anything you want.

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="ResponseCode")
    public JAXBElement<String> createResponseCode(String string) {
        return new JAXBElement<String>(new QName("ResponseCode"), String.class, string);
    }

    @XmlElementDecl(name="ResultCode")
    public JAXBElement<String> createResultCode(String string) {
        return new JAXBElement<String>(new QName("ResultCode"), String.class, string);
    }

}

演示代碼

input.xml

<responseAPI>
    <ResponseCode>ABC</ResponseCode>
</responseAPI>

演示

在創建 JAXBContext 時,我們需要確保包含包含 @XmlElementDecl 注釋的類.

When creating the JAXBContext we need to ensure that we include the class that contains the @XmlElementDecl annotations.

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(ResponseAPI.class, ObjectFactory.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("Scratch/src2/forum24554789/input.xml");
        ResponseAPI responseAPI = (ResponseAPI) unmarshaller.unmarshal(xml);

        ObjectFactory objectFactory = new ObjectFactory();
        String responseCode = responseAPI.getResponseCode().getValue();
        JAXBElement<String> resultCodeJAXBElement = objectFactory.createResultCode(responseCode);
        responseAPI.setResponseCode(resultCodeJAXBElement);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(responseAPI, System.out);
    }

}

輸出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<responseAPI>
    <ResultCode>ABC</ResultCode>
</responseAPI>

這篇關于@XmlElement 具有多個名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 亚洲一区二区三区桃乃木香奈 | 日日干夜夜操 | 久久精品视频9 | 国产一区二区在线免费观看 | 国产一区免费 | 欧美一级视频在线观看 | 久久成人免费视频 | 国产福利在线视频 | 亚洲欧美中文日韩在线v日本 | 久久精品成人 | 国产xxxx搡xxxxx搡麻豆 | 国产午夜精品一区二区三区嫩草 | 91偷拍精品一区二区三区 | 极情综合网 | 久草精品在线 | 色综合欧美 | 日韩一及片 | 久久国产精品偷 | 久久88| 天天操人人干 | 国产黄色大片在线观看 | 国产人成精品一区二区三 | 欧美国产日韩在线观看 | 欧美精品第一区 | 不卡视频一区二区三区 | 一级黄色影片在线观看 | 天天弄天天操 | www.天天干.com | 在线观看亚洲专区 | 欧美成人免费在线视频 | 久久亚洲欧美日韩精品专区 | 黄片毛片 | 久久久成人网 | 三级av免费 | 999久久久 | 91看片免费版 | 国产精品毛片一区二区在线看 | 欧美视频一级 | 欧美专区日韩 | 免费视频成人国产精品网站 | av片毛片 |