
本文旨在解决Spring Boot项目中,如何针对不同类型的报告生成需求,设计可扩展且易于维护的请求参数校验方案。通过抽象父类定义通用参数,子类继承并添加特定参数,结合Spring Validation框架,实现灵活的请求参数校验,避免代码冗余,提高代码复用率。
在实际的Spring Boot项目中,我们经常会遇到需要根据不同的请求参数生成不同类型的报告的场景。例如,一个报告生成接口可能需要根据category参数来决定生成哪种类型的报告,而不同类型的报告可能需要不同的请求参数。如何设计一个可扩展且易于维护的请求参数校验方案,就显得尤为重要。
一种常见的做法是定义一个抽象的请求参数类,其中包含所有报告类型都需要的通用参数,然后为每种报告类型定义一个具体的请求参数类,继承自抽象类,并添加该报告类型特有的参数。
1. 定义抽象请求参数类
首先,定义一个抽象类 ReportRequestDTO,其中包含所有报告类型都需要的通用参数,并使用JSR-303注解进行校验。
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public abstract class ReportRequestDTO {
@NotEmpty(message = "foo不能为空")
private String foo;
@NotEmpty(message = "bar不能为空")
private String bar;
}在这个例子中,foo 和 bar 是所有报告类型都需要的通用参数,@NotEmpty 注解表示这两个参数不能为空。message属性可以自定义校验失败时的提示信息。
2. 定义具体请求参数类
接下来,为每种报告类型定义一个具体的请求参数类,继承自 ReportRequestDTO,并添加该报告类型特有的参数,同样使用JSR-303注解进行校验。
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public class ReportADTO extends ReportRequestDTO {
@NotEmpty(message = "foobar不能为空")
private String foobar;
}import lombok.Data;
@Data
public class ReportBDTO extends ReportRequestDTO {
private Integer someNumber;
}ReportADTO 继承了 ReportRequestDTO,并添加了 foobar 参数,这个参数是 ReportA 特有的。ReportBDTO 继承了 ReportRequestDTO,并添加了 someNumber 参数,这个参数是 ReportB 特有的。
3. 在Controller中使用Validation
在 Controller 中,接收请求参数,并使用 @Valid 注解进行校验。关键在于如何根据category参数实例化对应的DTO。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import org.springframework.web.server.ResponseStatusException;
@RestController
@Validated // 需要添加此注解才能使@Valid生效
public class ReportController {
@GetMapping("/report")
@ResponseBody
public ResponseEntity<String> getReport(
@RequestParam(value = "category") String category,
@Valid @ModelAttribute ReportRequestDTO reportRequestDTO) {
if ("A".equals(category)) {
if (!(reportRequestDTO instanceof ReportADTO)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "category A requires ReportADTO");
}
ReportADTO reportADTO = (ReportADTO) reportRequestDTO;
// Process ReportA
return ResponseEntity.ok("Report A generated with foo: " + reportADTO.getFoo() + ", bar: " + reportADTO.getBar() + ", foobar: " + reportADTO.getFoobar());
} else if ("B".equals(category)) {
if (!(reportRequestDTO instanceof ReportBDTO)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "category B requires ReportBDTO");
}
ReportBDTO reportBDTO = (ReportBDTO) reportRequestDTO;
// Process ReportB
return ResponseEntity.ok("Report B generated with foo: " + reportBDTO.getFoo() + ", bar: " + reportBDTO.getBar() + ", someNumber: " + reportBDTO.getSomeNumber());
} else {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid category");
}
}
}在这个例子中,我们使用了 @Valid 注解来校验 ReportRequestDTO 对象。如果校验失败,Spring Boot 会自动抛出一个 MethodArgumentNotValidException 异常。
重要提示:
4. 全局异常处理
为了更好地处理校验失败的情况,可以定义一个全局异常处理类,处理 MethodArgumentNotValidException 异常,并返回统一的错误信息。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
errors.put(error.getField(), error.getDefaultMessage());
});
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
}总结
通过定义抽象请求参数类和具体请求参数类,结合Spring Validation框架,我们可以实现一个可扩展且易于维护的请求参数校验方案。这种方案可以避免代码冗余,提高代码复用率,并使代码更加清晰易懂。同时,全局异常处理可以提供统一的错误信息,提高用户体验。
注意事项
这种方法允许你在不修改现有端点的情况下添加新的报告类型,只需创建新的DTO类并实现相应的报告生成逻辑即可,提高了代码的可维护性和可扩展性。
以上就是Spring Validation:基于抽象请求参数类的灵活校验方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号