还差回退机制

This commit is contained in:
shenquanyi
2025-12-05 18:15:08 +08:00
parent 1ba3fb0c85
commit 5c8e828c60
6 changed files with 790 additions and 773 deletions

View File

@@ -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);
}

View File

@@ -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格式是否正确");
}
}
/**
* 处理业务异常
*/