add xml document parsing; clean up typos
This commit is contained in:
parent
87253ab4ff
commit
92a4247cd3
@ -5,9 +5,18 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -18,7 +27,18 @@ import java.util.Objects;
|
||||
public class MockController {
|
||||
|
||||
@PostMapping(path ="/call1")
|
||||
public String postCall1(@RequestBody String xmlRequestBody){
|
||||
public String postCall1(@RequestBody String xmlRequestBody) throws Exception {
|
||||
Document xmlRequest = parseRequestBody(xmlRequestBody);
|
||||
|
||||
Element root = xmlRequest.getDocumentElement();
|
||||
NodeList nodeList = root.getChildNodes();
|
||||
System.out.println(root.getNodeName());
|
||||
|
||||
for (int i = 0; i < nodeList.getLength(); i++){
|
||||
System.out.println(nodeList.item(i).getNodeName());
|
||||
System.out.println(nodeList.item(i).getNodeValue());
|
||||
}
|
||||
|
||||
return getStaticResponse("soap-example.xml");
|
||||
}
|
||||
|
||||
@ -29,7 +49,9 @@ public class MockController {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
|
||||
try {
|
||||
file = new File(Objects.requireNonNull(classLoader.getResource(filePath)).getFile());
|
||||
file = new File(
|
||||
Objects.requireNonNull(classLoader
|
||||
.getResource(filePath)).getFile());
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();}
|
||||
try {
|
||||
@ -39,4 +61,13 @@ public class MockController {
|
||||
e.printStackTrace();}
|
||||
return content;
|
||||
}
|
||||
|
||||
private Document parseRequestBody(String xmlRequestBody) throws Exception {
|
||||
DocumentBuilderFactory dBfactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = dBfactory.newDocumentBuilder();
|
||||
Document document =
|
||||
builder.parse(new InputSource(new StringReader(xmlRequestBody)));
|
||||
document.getDocumentElement().normalize();
|
||||
return document;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version = "1.0 encoding="utf-8""?>
|
||||
<?xml version = "1.0" encoding="utf-8" ?>
|
||||
<SOAP-ENV:Envelope
|
||||
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
|
||||
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
|
||||
|
Loading…
Reference in New Issue
Block a user