When we get input from any other Service/API in our own microservice, how our spring-boot application will know that the input type is either XML type or JSON type payload internally. And. After it is getting the actual kind of payload as input if I want to convert from XML to JSON or vice-versa, how it will work, will it have the same native method as below;
public static String xml= "<?xml version=\"1.0\" ?><root><test attribute=\"text1\">XML DATA</test><test attribute=\"text2\">DATA DATA</test></root>";
JSONObject json = XML.toJSONObject(xml);
String jsonString = json.toString(4);
System.out.println(jsonString);
//Convert from XML to JSON
and while converting from XML to JSON,
JSONObject jsonObject = new JSONObject(jsonString);
String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n<" root ">" XML.toString(jsonObject) "</" root ">";
Or is there any other way to convert?
Simply my query is like, how to know what is the input type and how our application will recognize this?
CodePudding user response:
You can read the content type request header as an argument in your controller's class mapping method:
public class APIController {
@GetMapping(
public String getMethod(@RequestBody String body, @RequestHeader("Content-type") String contentType) {
}
}