还差回退机制
This commit is contained in:
@@ -392,7 +392,7 @@ public class CattleDataController {
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Result<CattleData> createCattleData(
|
||||
@ApiParam(value = "牛只数据信息", required = true)
|
||||
@Valid @RequestBody CattleDataDTO dto) {
|
||||
@Valid @ModelAttribute CattleDataDTO dto) {
|
||||
CattleData data = cattleDataService.createCattleData(dto);
|
||||
return Result.success("创建成功", data);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.example.cattletends.common.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -37,6 +38,22 @@ public class GlobalExceptionHandler {
|
||||
return Result.error(400, "参数校验失败", errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求体缺失或格式错误异常
|
||||
*/
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
|
||||
String message = ex.getMessage();
|
||||
if (message != null && message.contains("Required request body is missing")) {
|
||||
logger.warn("请求体缺失: {}", ex.getMessage());
|
||||
return Result.error(400, "请求体不能为空,请提供JSON格式的请求数据");
|
||||
} else {
|
||||
logger.warn("请求体格式错误: {}", ex.getMessage());
|
||||
return Result.error(400, "请求体格式错误,请检查JSON格式是否正确");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理业务异常
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user