将XML转换为JSON,反之亦然,以及在发出XML请求时如何识别REST端点?

问题描述 投票:0回答:1

在发布此问题之前,我做了很多研究,但是没有得到正确的指导。我的公司已通过Proxy/Gateway将REST端点公开给外部世界。大约有1000位客户加入了Proxy / Gateway,并向基于SOAP的系统发出XML请求(部署在WebSphere App Server(WAS)V7.5上)。现在,我们已经开发了仅支持JSON的新系统,并希望指向部署在PCF(Pivotal Cloud Foundry)上的新开发系统。

这里我们不想让消费者进行任何更改。

[现在,我们正在尝试开发将XML请求转换为JSON的Adapter (Spring Boot Project),向新系统发出请求并获得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>ICA</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":""}}

任何指导?

java json xml spring-boot
1个回答
0
投票

这里是一种通过修改适配器代码来完成此操作的方法

如果要配置适配器以容纳端点映射,则可以使用@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);
    }
}

GET拨打http://localhost:8080/endpoints?endpoint=endpoint1电话>

您应该得到

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

这样,您可以使用EndpointRegistry获取所需的URL并进行呼叫。

您可以使用Spring Configuration服务器来更新每个环境的application.properties。在这里检查:https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

© www.soinside.com 2019 - 2024. All rights reserved.