简体   繁体   English

使用java Spring在post方法中使用一个RequestBody来区分xml

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

I was writing restApplication using Java Spring boot. 我正在使用Java Spring Boot编写restApplication。 I should write post request in spring. 我应该在春天写职位要求。 Which accept data is text/xml. 接受数据的是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 但是post方法的参数中的Dto可以例如在以下视图中一次更改类的名称

   <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. 如何一次为多个通用xml编写一个post方法。 Is this possible to do in Java spring ??. 在Java spring中可以做到吗? I saw in pyton it is possible to write just assign to variable some response.data and that's it 我在pyton中看到可以写一些赋给变量的response.data,仅此而已

   @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 现在我从字符串

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. 经过1天的搜索,我发现Response应该返回字符串,并且在Document类的帮助下,我们可以从字符串中解析新的xml,然后我们可以做我们应该做的事情.Request1是其他请求中的其他xml数据,它来自Other1 xml数据无关紧要。

 @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");

}

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. 希望这可以帮助您理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM