Spring 休息控制器不验证我的 DTO

Spring rest controller does not validate my DTO

我有这个请求和响应:

@Data
public class TestRequestDto {
    @Min(7)
    private String name;
}

@Data
public class TestResponseDto {
    private String response;
}

我有一个控制器:

package com.example.validation.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
public class TetController {

    @PostMapping("/test")
    public TestResponseDto getTestResponseDto(@Valid @RequestBody TestRequestDto request){
        log.info(request.getName());
        TestResponseDto response = new TestResponseDto();
        response.setResponse("response");
        return response;
    }
}

我发送了一个 post request({"name":"test"}) ,其中的名称无效,但它有效。我做错了什么?

从 Boot 2.3 开始,我们还需要显式添加 spring-boot-starter-validation 依赖项