|
@@ -1,11 +1,16 @@
|
|
|
package cn.com.qmth.markingaudit.support;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
import org.springframework.http.server.ServerHttpRequest;
|
|
|
import org.springframework.http.server.ServerHttpResponse;
|
|
|
+import org.springframework.http.server.ServletServerHttpRequest;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
@@ -15,6 +20,9 @@ import com.qmth.boot.api.exception.ExceptionResponseEntity;
|
|
|
@RestControllerAdvice
|
|
|
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
|
|
|
|
|
+ @Value("${server.servlet.context-path:}")
|
|
|
+ private String contextPath;
|
|
|
+
|
|
|
@Override
|
|
|
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
return true;
|
|
@@ -24,17 +32,29 @@ public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
|
|
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
|
|
|
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
|
|
ServerHttpResponse response) {
|
|
|
- response.setStatusCode(HttpStatus.OK);
|
|
|
- if (body instanceof String) {
|
|
|
- return JSON.toJSONString(Result.of(body));
|
|
|
- }
|
|
|
if (body instanceof Result) {
|
|
|
return body;
|
|
|
+ } else {
|
|
|
+ String apiUrl;
|
|
|
+ if (StringUtils.isNotEmpty(contextPath) && !"/".equals(contextPath)) {
|
|
|
+ apiUrl = contextPath + CustConstants.URI_PREFIX;
|
|
|
+ } else {
|
|
|
+ apiUrl = CustConstants.URI_PREFIX;
|
|
|
+ }
|
|
|
+ HttpServletRequest httpServletRequest = ((ServletServerHttpRequest) request).getServletRequest();
|
|
|
+ String url = httpServletRequest.getRequestURI();
|
|
|
+ if (!url.startsWith(apiUrl)) {
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+ response.setStatusCode(HttpStatus.OK);
|
|
|
+ if (body instanceof String) {
|
|
|
+ return JSON.toJSONString(Result.of(body));
|
|
|
+ }
|
|
|
+ if (body instanceof ExceptionResponseEntity) {
|
|
|
+ ExceptionResponseEntity t = (ExceptionResponseEntity) body;
|
|
|
+ return new Result<Object>(500, t.getMessage());
|
|
|
+ }
|
|
|
+ return Result.of(body);
|
|
|
}
|
|
|
- if (body instanceof ExceptionResponseEntity) {
|
|
|
- ExceptionResponseEntity t = (ExceptionResponseEntity) body;
|
|
|
- return new Result<Object>(500, t.getMessage());
|
|
|
- }
|
|
|
- return Result.of(body);
|
|
|
}
|
|
|
}
|