mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-29 16:13:47 +00:00
更新
This commit is contained in:
-43
@@ -1,43 +0,0 @@
|
||||
package org.itkk.udf.demo.server.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class ExceptionControllerAdvice {
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*
|
||||
* @param req 请求对象
|
||||
* @param e 异常对象
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<?> jsonErrorHandler(HttpServletRequest req, Exception e) {
|
||||
log.error("rest请求错误:", e);
|
||||
HttpStatus status = getStatus(req);
|
||||
return new ResponseEntity<>(e.getClass().getName(), status);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得错误响应状态
|
||||
*
|
||||
* @param request request
|
||||
* @return HttpStatus
|
||||
*/
|
||||
private HttpStatus getStatus(HttpServletRequest request) {
|
||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
if (statusCode == null) {
|
||||
return HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
return HttpStatus.valueOf(statusCode);
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package org.itkk.udf.demo.server.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class ExceptionHandle extends ResponseEntityExceptionHandler {
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*
|
||||
* @param req 请求对象
|
||||
* @param e 异常对象
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handle(WebRequest req, Exception e) {
|
||||
HttpStatus httpStatus = null;
|
||||
HttpHeaders headers = null;
|
||||
Object body = null;
|
||||
try {
|
||||
ResponseEntity<Object> objectResponseEntity = handleException(e, req);
|
||||
httpStatus = objectResponseEntity.getStatusCode();
|
||||
headers = objectResponseEntity.getHeaders();
|
||||
body = objectResponseEntity.getBody();
|
||||
} finally {
|
||||
return this.handleExceptionInternal(e, body, headers, httpStatus, req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||
return super.handleExceptionInternal(ex, body, headers, status, request);
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -16,10 +16,9 @@ public class DemoController {
|
||||
@Autowired
|
||||
private DemoService demoService;
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, path = "t1/{name}")
|
||||
public DemoEntity t1(@RequestBody DemoEntity demoEntity, @PathVariable String name) {
|
||||
log.info("demoEntity:", demoEntity);
|
||||
//demoService.t1();
|
||||
@RequestMapping(method = RequestMethod.GET, path = "t1/{name}")
|
||||
public DemoEntity t1(@PathVariable String name) {
|
||||
demoService.t1();
|
||||
return new DemoEntity(name, new Date());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user