簡體   English   中英

將 XML 轉換為 JSON,反之亦然,以及如何在發出 XML 請求時識別 REST 端點?

[英]Convert XML to JSON and Vice Versa and also how to identify REST endpoint while making XML Request?

在發布這個問題之前,我做了很多研究,但是我沒有得到適當的指導。 我的公司已通過Proxy/Gateway將 REST 端點暴露給外部世界。 大約有 1000 名客戶加入了代理/網關,並向基於 SOAP 的系統(部署在WebSphere App Server (WAS) V7.5 上)發出 XML 請求。 現在,我們開發了僅支持 JSON 的新系統,並希望指向部署在 PCF(Pivotal Cloud Foundry)上的新開發系統。

在這里我們不想要求消費者做任何改變。

現在,我們正在嘗試開發Adapter (Spring Boot Project) ,它將 XML 請求轉換為 JSON 向新系統發出請求並以 JSON 獲得響應,然后Adapter (Spring Boot Project)再次將 JSON 轉換為 XML。 在這里,JSON 響應和 XML 響應有時可能不同。

現在,我真的無法決定調用哪個端點

String xml_data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ahc.com:dms:wsdls:organization\">\n" + 
        "   <soapenv:Header/>\n" + 
        "   <soapenv:Body>\n" + 
        "      <urn:getRoles>\n" + 
        "         <getRolesRequest>\n" + 
        "            <Type>ABC</Type>\n" + 
        "         </getRolesRequest>\n" + 
        "      </urn:getRoles>\n" + 
        "   </soapenv:Body>\n" + 
        "</soapenv:Envelope>";
JSONObject obj = XML.toJSONObject(xml_data);
System.out.println(obj);

它給了我以下回應。

{"soapenv:Envelope":{"soapenv:Body":{"urn:getRoles":{"getRolesRequest":{"Type":"AYU"}}},"xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:urn":"urn:ahc.com:dms:wsdls:organization","soapenv:Header":""}}

任何指導?

注意:我想從 JSON 對象中刪除<soapenv:Envelope xmlns:soapenv=\\"http://schemas.xmlsoap.org/soap/envelope/\\ 。另外,在將 JSON 轉換為 XML 時,我想添加<soapenv:Envelope xmlns:soapenv=\\"http://schemas.xmlsoap.org/soap/envelope/\\

您可以使用underscore-java庫。 它具有讀取 xml 和生成 json 的靜態方法。

    String xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""
    + " xmlns:urn=\"urn:ahc.com:dms:wsdls:organization\">\n"
    + "   <soapenv:Header/>\n"
    + "   <soapenv:Body>\n"
    + "      <urn:getRoles>\n"
    + "         <getRolesRequest>\n"
    + "            <Type>ABC</Type>\n"
    + "         </getRolesRequest>\n"
    + "      </urn:getRoles>\n"
    + "   </soapenv:Body>\n"
    + "</soapenv:Envelope>";
    Map<String, Object> jsonData = U.<Map<String, Object>>get(
        U.<Map<String, Object>>fromXmlWithoutNamespaces(xmlData), "Envelope.Body.getRoles");
    System.out.println(U.toJson(jsonData));

輸出:

{
  "getRolesRequest": {
    "Type": "ABC"
  }
}

如果您的網關支持為每個端點傳遞自定義標頭,那將是執行此操作的理想方式。 在這種情況下,它將在請求標頭中發送 REST 端點,您可以使用它來轉發調用。

如果網關不支持此類功能,您可以嘗試修改您的Adapter代碼。

如果要配置適配器以保存端點映射,可以使用@ConfigurationProperties進行配置

EndpointRegistry

import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

@EnableConfigurationProperties
@ConfigurationProperties(prefix = "endpoint")
@Component
public class EndpointRegistry {

    // top key - endpoint
    // one key - soapUrl
    // another key - restUrl
    private Map<String, Map<String, String>> api;

    public Map<String, Map<String, String>> getApi() {
        return api;
    }

    public void setApi(Map<String, Map<String, String>> api) {
        this.api = api;
    }
}

這將匹配您的application.properties文件中提到的密鑰模式,如下所示

endpoint.api.endpoint1.soapUrl=<soap-url>
endpoint.api.endpoint1.restUrl=<rest-url>

endpoint.api.endpoint2.soapUrl=<soap-url>
endpoint.api.endpoint2.restUrl=<rest-url>

現在, endpoint name應該是唯一的。 您可以決定密鑰結構。 這只是一個例子。

這是一個用於驗證值的測試控制器:

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/endpoints")
public class EndpointController {

    @Autowired
    EndpointRegistry registry;

    @GetMapping
    public Map<String, String> getRestUrl(String endpoint) {
        return registry.getApi().get(endpoint);
    }
}

http://localhost:8080/endpoints?endpoint=endpoint1進行GET調用

你應該得到

{
   "soapUrl":"<soap-url>",
   "restUrl":"<rest-url>"
}

這樣,您可以使用EndpointRegistry獲取所需的 URL 並進行調用。

您可以使用 Spring 配置服務器為每個環境更新您的 application.properties。 在這里檢查: https : //cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

同時,您可以配置 CI 管道以預先為每個環境添加這些屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM