soapmock/src/main/java/com/gmgauthier/soapmock/controllers/MockController.java

43 lines
1.4 KiB
Java

package com.gmgauthier.soapmock.controllers;
import org.springframework.http.MediaType;
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 java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;
@RestController
@RequestMapping(
consumes = MediaType.TEXT_XML_VALUE,
produces = MediaType.TEXT_XML_VALUE)
public class MockController {
@PostMapping(path ="/call1")
public String postCall1(@RequestBody String xmlRequestBody){
return getStaticResponse("soap-example.xml");
}
private String getStaticResponse(String fileName){
File file = null;
String content = null;
String filePath = "static/responses/" + fileName;
ClassLoader classLoader = getClass().getClassLoader();
try {
file = new File(Objects.requireNonNull(classLoader.getResource(filePath)).getFile());
} catch (NullPointerException e) {
e.printStackTrace();}
try {
assert file != null;
content = new String(Files.readAllBytes(file.toPath()));
} catch (IOException e) {
e.printStackTrace();}
return content;
}
}