将动态XML/JSON内容与静态标记化负载进行比较,并检索标记值 [英] Compare dynamic XML/JSON content with static tokenised payload and retrieve token values

查看:68
本文介绍了将动态XML/JSON内容与静态标记化负载进行比较,并检索标记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现模拟http响应服务器.服务器必须验证输入的请求URL和有效负载,然后将请求与已配置的响应进行匹配,然后将其返回给调用方.

I am implementing mock http response server. The server has to validate the input request url and payload then match the request to configured response then return it back to the caller.

我需要帮助来验证带有静态标记化有效负载的http请求动态内容有效负载. 因此,当我收到请求有效载荷时说json时,将其与配置的标记化内容进行比较,如果不匹配则返回失败.

In that i need help on validating the http request dynamic content payload with static tokenised payload. So when i got the request payload say json, compare it with configured tokenised content, and return failure if it not matches.

例如,我正在使用以下代码对请求网址进行同样的操作.

e.g) I am doing the same for request url with below code.

import java.util.HashMap;
import java.util.Map;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;


    public static void main(String[] args) {
        //template url
        String template = "/name/{name}/age/{age}";
        UriTemplate uriTemplate = new UriTemplate(template);

        //actual url
        String uri = "/name/Bob/age/47";
        Map<String, String> parameters = new HashMap<>();

        //returns Map
        System.out.println("Dynamic Content Map: " + uriTemplate.match(uri));
        System.out.println("URL Matched: " +uriTemplate.matches(uri));

        parameters.put("name", "Foo");
        parameters.put("age", "37");

        UriComponentsBuilder builder = UriComponentsBuilder.fromPath(template);
        System.out.println(builder.buildAndExpand(parameters).toUriString());

    }

输出:

Dynamic Content: {name=Bob, age=47}
URL Matched: true
/name/Foo/age/37

因此,如果您看一下这段代码,UriTemplate能够比较配置了动态值(Bob/47)的静态内容(名称/年龄).

So if you look at this code, UriTemplate is capable of comparing the static content (name/age) configured with dynamic value (Bob/47) filled content.

我想在请求有效负载中执行相同的比较.现在的挑战是

The same comparison i want to do it in request payload. Now challenges are

  1. 内容可以是XML或JSON,后面还有其他内容.
  2. 内容可能在
  3. 之间包含空格
  4. 顺序将不同或XML名称空间不同
  5. 它将包含要与静态比较的动态变量值
  6. 如何从有效载荷中检索动态变量值
  1. Content may be XML or JSON, later something else.
  2. Content may contains spaces in between
  3. Order will be different OR different xml name space
  4. It will contain dynamic variable values to compare with static
  5. How to retrieve the dynamic variable values from the payload

我知道我可以使用XML和JSON解析器进行比较,但是如何将静态变量与内容内部的动态变量进行比较并检索呢?

I know i can go with XML and JSON parser to compare, but how to compare static with dynamic variables inside the content and retrieve it?

例如e)静态{"name" : "$name", "age" : "$age"}

e.g) Static {"name" : "$name", "age" : "$age"}

例如,动态{"name" : "Bob", "age" : 47}

有没有我可以传递静态和动态内容的工具,并且我将获得isMatched并在如上所示的uriTemplate示例的地图中检索动态常量?

Is there any tool i can pass both static and dynamic content, and i will get isMatched and retrieve the dynamic constants in a map like above shown uriTemplate examples?

在比较和提取动态字段方面给我一些提示/想法吗?

Give me some hints/ideas on comparing and extracting dynamic fields?

推荐答案

XML和JSON是结构的序列化表示.

XML and JSON are serialized representations of a structure.

您引用的动态内容实际上是该结构的一个实例.

The dynamic content that you refer is actually an instance of that structure.

我认为您正在寻找的是XML的XSD/DTD [1](定义结构的类型)和JSON的json-schema [3].

What I think you're looking for is XSD/DTD [1] (define the type of your structure) for XML and json-schema [3] for JSON.

这里有多种策略.取决于要验证的服务.您可以将json转换为xml,并使用相同的XSD来验证这两种序列化方法.有多种框架可以帮助您实现这一目标.但是,第一步是编写这些模式(对于XML为XSD/DTD,对于JSON为json-schema).

There are multiple strategies here. Depending on the service to be validated. You can convert json to xml and use the same XSD to validate both serialization methods. There are various frameworks to help you achieve this. However, the first step would be to write those schemas (XSD/DTD for XML and/or json-schema for JSON).

  • [1] - wiki: XML schema
  • [2] - What is the difference between XML and XSD?
  • [3] - json-schema
  • [4] - How do I validate incoming JSON data inside a REST service?

这篇关于将动态XML/JSON内容与静态标记化负载进行比较,并检索标记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆