Spring Boot 参数注解校验

项目backend模块Controller层和api模块service层使用注解校验
1、校验单参数

  • 在对应类上加@Validated注解
  • 在接口参数上添加校验注解
    注意是在类上添加@Validated
@RestController
@Validated
public class ResourceController extends BaseController {
    @GetMapping("/resourceCenter/paper/analyseByDifficulty")
    public List<DifficultyAnalyseInfo> analyseByDifficulty(@NotNull(message = "paperId不能为空") Long paperId) {
        return resourceCenterService.analyseByDifficulty(paperId);
    }
}
@FeignClient(name = "cloud-aital-resource-svc", decode404 = true, url = "${feign.url.cloud-aital-resource-svc:}")
@Validated
public interface ResourceService {
    @GetMapping("/resource/file")
    ApiResult<EcpFileDTO> getResourceFile(@NotNull(message = "fileId不能为空") Long fileId);
}

2、对引用类型(封装参数对象)参数校验

  • 在对象参数需要校验的属性上添加校验注解
  • 在接口参数上添加@Valid注解
public class HtmlDownloadRequest implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 下载html代码
     */
    @NotBlank(message = "html不能为空!")
    private String html;
    /**
     * 下载文件类型,pdf,docx
     */
    @NotBlank(message = "fileType不能为空!")
    private String fileType;
    /**
     * 业务id
     */
    @NotNull(message = "业务id不能为空!")
    private Long id;
    /**
     * 对象或集合属性级联校验需要加@Valid注解
     */
    @Valid
    private EcpFileDTO files;
}
@PostMapping("/resourceCenter/downloadQuestionOrPaper")
public EcpFileDTO downloadQuestionOrPaper(@RequestBody @Valid HtmlDownloadRequest request) {
    return resourceCenterService.getHtmlDownloadUrl(request);
}

常见的注解校验
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Spring Boot Validation来对参数名称进行校验。通过引入spring-boot-starter-validation依赖,可以使用Spring Validator对参数进行校验Spring Validator是对Hibernate Validator的进一步封装,同时支持Spring MVC的自动校验。你可以在Spring官方文档中找到更多关于Spring Boot Validation的详细信息\[2\]。 在使用Spring Boot Validation时,可以使用@Validated注解来标记需要校验的类型、方法和方法参数。@Validated注解属于org.springframework.validation.annotation包,是Spring校验机制之一。它具有分组校验的功能,可以用于类型、方法和方法参数上,但不能用于成员属性(field)\[3\]。 通过使用Spring Boot Validation,你可以简化参数校验的代码,提高代码的可读性和美观性。 #### 引用[.reference_title] - *1* *3* [Spring Boot参数校验](https://blog.csdn.net/qq1929892209/article/details/126133350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring Boot 参数校验校验工具类](https://blog.csdn.net/WEDUEST/article/details/121594610)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值