응답지연시간 / 응답 status code 설정 기능 추가

This commit is contained in:
hsoh
2021-07-11 02:05:56 +09:00
parent 10b5f9fa78
commit 300a6b420c
5 changed files with 79 additions and 5 deletions
-1
View File
@@ -15,7 +15,6 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
@@ -30,7 +30,8 @@ public class ApiController {
private ApiService apiService;
@SuppressWarnings("deprecation")
@RequestMapping(value = "/mock-api/**")
//@RequestMapping(value = "/mock-api/**")
@RequestMapping(value = "/{_:^(?!plugins|dist|favicon.ico).*$}/**")
public ResponseEntity<String> mockApi(HttpEntity<String> httpEntity, HttpServletRequest request,
HttpServletResponse response) {
System.out.println("==================================================");
@@ -67,7 +68,25 @@ public class ApiController {
String.format("Method(%s) not allowed", request.getMethod()));
}
}
// sleep Àû¿ë
if(api.getSleepSeconds() != null) {
int sleepSeconds = api.getSleepSeconds().intValue();
if (sleepSeconds > 0) {
try {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(String.format("Start Sleep %d Seconds.", sleepSeconds));
System.out.println("...");
Thread.sleep(sleepSeconds * 1000);
System.out.println("......");
System.out.println(String.format("Finished Sleep %d Seconds.", sleepSeconds));
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
HttpHeaders responseHeaders = new HttpHeaders();
if (StringUtils.isNotBlank(api.getResponseContentType())) {
responseHeaders.setContentType(MediaType.parseMediaType(api.getResponseContentType()));
@@ -76,8 +95,15 @@ public class ApiController {
}
assignResponseHeaders(responseHeaders, api.getResponseHeaders());
// status code Àû¿ë
int responseStatusCode = 200;
if (api.getResponseStatusCode() != null) {
responseStatusCode = api.getResponseStatusCode().intValue();
System.out.println("Response Status Code=" + responseStatusCode);
}
return new ResponseEntity<String>(assignResponseBody(api), responseHeaders, HttpStatus.OK);
return new ResponseEntity<String>(assignResponseBody(api), responseHeaders, responseStatusCode);
}
private String assignServiceValue(HttpEntity<String> httpEntity, HttpServletRequest request, String url) {
@@ -10,7 +10,9 @@ import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
@SuppressWarnings("serial")
@Entity
@@ -46,6 +48,12 @@ public class ApiInfo implements Serializable {
@Column(length = 5000)
private String responseHeaders;
@Digits(fraction = 0, integer = 3)
private Integer responseStatusCode;
@Positive
private Integer sleepSeconds;
@Column(length = 5000)
private String responseBody1;
@@ -116,6 +124,22 @@ public class ApiInfo implements Serializable {
this.responseHeaders = responseHeaders;
}
public Integer getResponseStatusCode() {
return responseStatusCode;
}
public void setResponseStatusCode(Integer responseStatusCode) {
this.responseStatusCode = responseStatusCode;
}
public Integer getSleepSeconds() {
return sleepSeconds;
}
public void setSleepSeconds(Integer sleepSeconds) {
this.sleepSeconds = sleepSeconds;
}
public String getResponseBody1() {
return responseBody1;
}
@@ -136,7 +160,8 @@ public class ApiInfo implements Serializable {
public String toString() {
return "ApiInfo [id=" + id + ", apiGroupName=" + apiGroupName + ", apiName=" + apiName + ", url=" + url
+ ", serviceValue=" + serviceValue + ", method=" + method + ", responseContentType="
+ responseContentType + ", responseHeaders=" + responseHeaders + ", responseBody1=" + responseBody1
+ responseContentType + ", responseHeaders=" + responseHeaders + ", responseStatusCode="
+ responseStatusCode + ", sleepSeconds=" + sleepSeconds + ", responseBody1=" + responseBody1
+ ", responseBody2=" + responseBody2 + "]";
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

@@ -129,6 +129,30 @@
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="col-sm-3 col-form-label">응답 Status Code</label>
<div class="col-sm-9">
<input type="text" th:field="*{responseStatusCode}"
class="form-control"> <span
th:if="${#fields.hasErrors('responseStatusCode')}" th:errors="*{responseStatusCode}"
class="text-danger"></span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Sleep 타임(초)</label>
<div class="col-sm-9">
<input type="text" th:field="*{sleepSeconds}"
class="form-control"> <span
th:if="${#fields.hasErrors('sleepSeconds')}" th:errors="*{sleepSeconds}"
class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group row">