在使用JavaSpring的post方法中使用一个RequestBody来区分xml
我正在使用JavaSpringBoot编写重新启动应用程序。我应该在春天写一封请帖。接受的数据是text/xml。然而,post方法参数中的Dto可以更改类的名称,例如,当它出现在下面的视图中时在使用JavaSpring的post方法中使用一个RequestBody来区分xml,java,xml,spring,rest,Java,Xml,Spring,Rest,我正在使用JavaSpringBoot编写重新启动应用程序。我应该在春天写一封请帖。接受的数据是text/xml。然而,post方法参数中的Dto可以更改类的名称,例如,当它出现在下面的视图中时 <Request1> <Head> <head> <id/> <name/> <surname/> </head> </Head> </Request1> 在相
<Request1>
<Head>
<head>
<id/>
<name/>
<surname/>
</head>
</Head>
</Request1>
在相同url地址的请求中,它可能出现在其他视图中
<Other1>
<Head>
<head>
</fio>
</head>
</Head>
</Other1>
如何一次为多个通用XML编写一个post方法。在JavaSpring中可以做到这一点吗??。我在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);
}
@RequestMapping(name=“/a”,method=RequestMethod.POST,products=MediaType.ALL\u值)
private ResponseEntity get(@RequestBody String data)抛出ParserConfiguration异常、IOException、SAXException{
字符串temp=“”;
对于(int i=0;i
现在,我从字符串中得到了好的,当响应中有不同的数据时,您想得到不同的结果吗? 如果是,请处理控制器内的视图,您希望为该响应显示哪个视图
我希望这能帮助您理解。经过一天的搜索,我发现响应应该以字符串形式返回,在Document类的帮助下,我们可以从字符串解析新的xml,然后我们可以做我们应该做的事情。Request1是其他请求中的第一个xml数据,它来自其他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");
}
@RequestMapping(name=“/a”,method=RequestMethod.POST,products=MediaType.ALL\u值)
private ResponseEntity getIt(@RequestBody String path)抛出ParserConfiguration异常、IOException、SAXException{
Document doc=DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(新输入源(新StringReader(路径));
if(path.contains(“Request1”)){
节点列表标记名=doc.getElementsByTagName(“id”);
如果(标记名.getLength()>0){
System.out.println(标记名.item(0).getTextContent());
}
}
if(path.contains(“Other1”)){
节点列表标记名=doc.getElementsByTagName(“fio”);
如果(标记名.getLength()>0){
System.out.println(标记名.item(0).getTextContent());
}
}
返回ResponseEntity.ok(“已保存”);
}
检查Jackson Parsing这个不同的东西我想使用一个ResponseBody,它在Post请求中提供不同类型的xml您最好为不同的类提供两个不同的URL,这将xml转换为不同的对象。但是你可能有一个共同的功能(可能使用泛型),它将以相同的方式处理几种不同类型的对象