Spring启动:控制器不考虑验证器

Spring Boot: Controller doesn't take in account Validator

我正在尝试将验证器用于控制器。 origindestination这两个字段应该是三个大写字母作为IATA代码。但它没有过滤器,任何请求都会被接受。

验证器接口集:

@Documented
@Constraint(validatedBy = IATACodeValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IATACodeConstraint {
    String message() default "Invalid IATA code";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

Class 个验证者:

public class IATACodeValidator implements ConstraintValidator<IATACodeConstraint, String> {

@Override
public void initialize(IATACodeConstraint iataCode) {
}

@Override
public boolean isValid(String codeField, ConstraintValidatorContext context) {
    return codeField != null && codeField.matches("^[A-Z]{3}+$")
            && (codeField.length() == 3);
}

基本class:

public class CrazyAirRequest {

    @IATACodeConstraint
    private String origin;
    @IATACodeConstraint
    private String destination;
    private String departureDate;
    private String returnDate;
    private int passengerCount;

// getters & setters

控制器:

@RestController
@RequestMapping("flights")
@Validated
public class BusyFlightsController {


    CrazyAirDatabase crazyAirService;
    ToughJetDatabase toughJetService;

    @Autowired
    public BusyFlightsController(CrazyAirDatabase crazyAirService, ToughJetDatabase toughJetService) {
        this.crazyAirService = new CrazyAirDatabase();
        this.toughJetService = new ToughJetDatabase();
    }


    @RequestMapping(value = "/crazy-air-response", method = RequestMethod.GET)
    public List<CrazyAirResponse> getCrazyAirResponse(
            @RequestParam("origin")  String origin,
            @RequestParam("destination")  String destination,
            @RequestParam("departureDate") String departureDate,
            @RequestParam("returnDate") String returnDate,
            @RequestParam("passengerCount") int passengerCount
    ) {
        CrazyAirRequest crazyAirRequest = new CrazyAirRequest(origin, destination, departureDate, returnDate,
                passengerCount);
        return crazyAirService.getCrazyAirResponse(crazyAirRequest);
    }

您可以直接使用@Pattern 注解而无需为两个字段创建自定义验证器,如下所示:

@Pattern(regexp ="^[A-Z]{3}" message ="Invalid IATA code")
private String origin;
@Pattern(regexp ="^[A-Z]{3}" message ="Invalid IATA code")
private String destination;

@virendra chauhan:谢谢你的启发。

我是这样解决的:

 @RequestMapping(value = "/crazy-air-response", method = RequestMethod.GET)
public List<CrazyAirResponse> getCrazyAirResponse(
        @RequestParam("origin") @Pattern(regexp = "^[A-Z]{3}", message = "Invalid IATA code")
                String origin,
        @RequestParam("destination") @Pattern(regexp = "^[A-Z]{3}", message = "Invalid IATA code")
                String destination,
        @RequestParam("departureDate") String departureDate,
        @RequestParam("returnDate") String returnDate,
        @RequestParam("passengerCount") int passengerCount
) {
    CrazyAirRequest crazyAirRequest = new CrazyAirRequest(origin, destination, departureDate, returnDate,
            passengerCount);
    return crazyAirService.getCrazyAirResponse(crazyAirRequest);
}