Commit 35ec20d0 authored by 赵啸非's avatar 赵啸非

添加返回http code 500错误

parent eecfa776
package com.mortals.xhx.base.framework.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mortals.xhx.base.framework.DateJacksonConverter;
import org.springframework.context.annotation.Bean;
......@@ -24,8 +25,12 @@ public class ConverterConfig {
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
return mappingJackson2HttpMessageConverter;
}
}
package com.mortals.xhx.base.framework.config;
import com.mortals.framework.filter.RepeatableFilter;
import com.mortals.framework.filter.XssFilter;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.framework.filter.RepeatReadHttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
......@@ -21,6 +21,7 @@ import java.util.Map;
* @author zxfei
*/
@Configuration
@Slf4j
public class FilterConfig {
@Value("${xss.enabled}")
private String enabled;
......@@ -60,7 +61,6 @@ public class FilterConfig {
public static class RequestReplaceFilter implements Filter {
@Override
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
}
@Override
......@@ -70,6 +70,7 @@ public class FilterConfig {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("RequestReplaceFilter~~~~~~~~~~~~~~~~~");
filterChain.doFilter(new RepeatReadHttpRequest((HttpServletRequest) servletRequest), servletResponse);
}
}
......
package com.mortals.xhx.base.framework.exception;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
......@@ -10,6 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 统一异常处理
*/
......@@ -17,6 +24,12 @@ import com.mortals.framework.exception.AppException;
@Slf4j
public class ExceptionHandle {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
public static final String KEY_RESULT_CODE = "code";
public static final String KEY_RESULT_MSG = "msg";
public static final String KEY_RESULT_DATA = "data";
......@@ -25,6 +38,9 @@ public class ExceptionHandle {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public String handle(Exception e) {
log.info("[request url]========{}", request.getRequestURL());
response.getStatus();
JSONObject ret = new JSONObject();
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
if (e instanceof AppException) {
......@@ -33,9 +49,15 @@ public class ExceptionHandle {
stack.getClassName(), stack.getMethodName(), stack.getLineNumber(), e.getClass().getName());
AppException ex = (AppException) e;
ret.put(KEY_RESULT_MSG, ex.getMessage());
}
if (e instanceof HttpMessageNotReadableException) {
log.error("[system error]", e);
response.setStatus(HttpStatus.HTTP_BAD_REQUEST);
ret.put(KEY_RESULT_MSG, "参数错误,"+ StrUtil.subBefore(e.getMessage(), ";", false));
} else {
log.error("[system error]", e);
ret.put(KEY_RESULT_MSG, "未知错误!");
response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
ret.put(KEY_RESULT_MSG, "未知错误!" + e.getMessage());
}
return ret.toJSONString();
}
......
......@@ -13,6 +13,7 @@ spring:
jackson:
serialization:
WRITE_DATES_AS_TIMESTAMPS: true
fail_on_empty_beans: true
default-property-inclusion: NON_NULL
# time-zone: GMT+8
# date-format: yyyy-MM-dd HH:mm:ss
......
......@@ -26,7 +26,7 @@ Content-Type: application/json
{
"siteId": 1,
"appTypeNotList": [4],
"page": 1,
"page": 123123123213123212312231231231,
"size": -1
}
###自助终端应用列表
......
......@@ -158,4 +158,4 @@ Content-Type: application/json
POST {{baseUrl}}/site/getSitesGroupByAreaLevel
Content-Type: application/json
{"areaLevel":3}
{"areaLevel":3,"siteName": "%翠屏区%"}
......@@ -208,13 +208,11 @@ Content-Type: application/x-www-form-urlencoded
businessid=125&matter=125&devicenum=C0-FB-F9-CD-3B-5D&peopleid=13
### 参数列表组合查询
POST {{baseUrl}}/param/list
POST {{baseUrl}}/param/interlist
Content-Type: application/json
{
"page":1,
"size": -1,
"firstOrganize": "Window",
"secondOrganize": "hongqi"
}
......
This diff is collapsed.
......@@ -181,7 +181,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
}
private void writeAccessLog(AccessLogPdu accessLogPdu) {
// log.info("accessLog:{}",JSON.toJSONString(accessLogPdu));
messageProducer.syncAccessSend(accessLogPdu);
}
......@@ -211,8 +210,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
if (contentLength > 0) {
httpHeaders.setContentLength(contentLength);
} else {
// TODO: this causes a 'HTTP/1.1 411 Length Required' // on
// httpbin.org
httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
}
return httpHeaders;
......@@ -220,8 +217,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
@Override
public Flux<DataBuffer> getBody() {
// log.info("outputMessage.getBody() contentLength:");
return outputMessage.getBody();
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment