我正在尝试为我的 Java Spring API 创建自定义验证规则。我有
CredentialCreateDTO
,它有一个名为 validRedirectUris
的字符串数组。我正在尝试检查 validRedirectUris
中的每个字符串是否都是有效的 URL。
这是我的代码:
package main.controller;
import main.dto.credentials.CredentialCreateDTO;
import main.model.Credential;
import main.response.BaseResponse;
import main.service.CredentialsService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/oauth/credentials")
public class CredentialsController {
private final CredentialsService credentialsService;
@Autowired
public CredentialsController(CredentialsService credentialsService) {
this.credentialsService = credentialsService;
}
@PostMapping("")
public ResponseEntity<BaseResponse<Credential>> createCredentials(@Valid @RequestBody CredentialCreateDTO credentialCreateDto) {
Credential credential = credentialsService.createCredentials(credentialCreateDto);
BaseResponse<Credential> response = new BaseResponse<Credential>(credential, "credential", "/oauth/credentials");
return ResponseEntity.ok(response);
}
}
package main.dto.credentials;
import main.util.ValidUrl;
import java.util.List;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import org.springframework.validation.annotation.Validated;
@Getter
@Setter
public class CredentialCreateDTO {
@NotNull(message = "validRedirectUris cannot be null")
@Size(min = 1, message = "validRedirectUris must have at least one element")
@ValidUrl(message = "Invalid URL")
private List<String> validRedirectUris;
@NotNull(message = "scopes cannot be null")
@Size(min = 1, message = "validRedirectUris must have at least one element")
private List<String> scopes;
// Constructor
public CredentialCreateDTO(List<String> validRedirectUris, List<String> scopes) {
this.validRedirectUris = validRedirectUris;
this.scopes = scopes;
}
// Default constructor for deserialization
public CredentialCreateDTO() {
}
}
package main.util;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = UrlValidator.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidUrl {
String message() default "Invalid URL";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
package main.util;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.net.URI;
import java.util.List;
public class UrlValidator implements ConstraintValidator<ValidUrl, List<String>> {
@Override
public void initialize(ValidUrl constraintAnnotation) {
}
@Override
public boolean isValid(List<String> urls, ConstraintValidatorContext context) {
if (urls == null) {
return true; // Use @NotNull if null is not allowed
}
for (String url : urls) {
try {
URI uri = new URI(url);
uri.toURL();
} catch (Exception e) {
return false;
}
}
return true;
}
}
我尝试通过添加 System.outs、断点等进行调试。
尽管编译和运行正确,但它似乎没有运行 .isValid 函数。
您的 @RestController 上缺少 @Validated 注释。如果没有它,SpringMVC 就不会验证方法的参数。