Jahongir  Sabirov
Jahongir Sabirov

Reputation: 470

use one RequestBody to differnet xml in post method using java Spring

I was writing restApplication using Java Spring boot. I should write post request in spring. Which accept data is text/xml. However Dto which comes in argument of post method can change name of class for example one time it might come in following view

   <Request1>
<Head>
<head>
    <id/>
    <name/>
    <surname/>
</head>
</Head>
</Request1>

in the request of the same url address it might come in other view

   <Other1>
<Head>
<head>
   </fio>
</head>
</Head>
</Other1>

How I can write one post method for several universal xmls in one time. Is this possible to do in Java spring ??. I saw in pyton it is possible to write just assign to variable some response.data and that's it

   @RequestMapping(name = "/a",method = RequestMethod.POST,produces = MediaType.ALL_VALUE)
    private ResponseEntity<String> get(@RequestBody String data) throws ParserConfigurationException, IOException, SAXException {
        String temp = "";
        for(int i = 0 ; i < data.length() ;i ++){
            if(Character.isAlphabetic(data.charAt(i))  || Character.isDigit(data.charAt(i)) || data.charAt(i) == '<' || data.charAt(i) == '>' || data.charAt(i) == '/' ){
                    temp += data.charAt(i);
            }
        }
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource src = new InputSource();
        src.setCharacterStream(new StringReader(temp));

        Document doc = builder.parse(src);
        String temp23 = doc.getDocumentURI();
        System.out.println(temp23);
        String id = doc.getElementsByTagName("id").item(0).getTextContent();
        String name = doc.getElementsByTagName("userName").item(0).getTextContent();
        //String pink = doc.getElementsByTagName("request").item(0).getTextContent();
        System.out.println(id+" "+name+" "+temp23);
        return ResponseEntity.ok(data);
    }

For now I get from string

Upvotes: 1

Views: 686

Answers (2)

Jahongir  Sabirov
Jahongir Sabirov

Reputation: 470

After 1 day search I figured out that Response should return in string and with help of Document class we could parse new xml from string then we can do what we should to do.Request1 is the first xml data in other request it comes from Other1 xml data it dosn't matter.

 @RequestMapping(name = "/a",method = RequestMethod.POST,produces = MediaType.ALL_VALUE)
private ResponseEntity<String> getIt(@RequestBody String path) throws ParserConfigurationException, IOException, SAXException {
    Document doc = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder()
            .parse(new InputSource(new StringReader(path)));
   if(path.contains("Request1")){
    NodeList tagName = doc.getElementsByTagName("id");
    if(tagName.getLength() > 0){
        System.out.println(tagName.item(0).getTextContent());
    }

   }
   if(path.contains("Other1")){
        NodeList tagName = doc.getElementsByTagName("fio");
        if(tagName.getLength() > 0){
            System.out.println(tagName.item(0).getTextContent());
        }
   }
    return ResponseEntity.ok("SAVED");

}

Upvotes: 1

Nahid
Nahid

Reputation: 98

Okay, Do you want to get the different result when you have different data in the response? If yes then please handle the view inside your controller which view do you want to show for that response.

I hope this will help you to understand.

Upvotes: 1

Related Questions