问题
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.
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());
}
OUTPUT:
Dynamic Content: {name=Bob, age=47}
URL Matched: true
/name/Foo/age/37
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
- Content may be XML or JSON, later something else.
- Content may contains spaces in between
- Order will be different OR different xml name space
- It will contain dynamic variable values to compare with static
- How to retrieve the dynamic variable values from the payload
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.g) Static {"name" : "$name", "age" : "$age"}
e.g) Dynamic {"name" : "Bob", "age" : 47}
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?
回答1:
XML and JSON are serialized representations of a structure.
The dynamic content that you refer is actually an instance of that structure.
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.
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?
来源:https://stackoverflow.com/questions/51146644/compare-dynamic-xml-json-content-with-static-tokenised-payload-and-retrieve-toke