简体   繁体   English

Spring Boot-NotEmpty注解仅适用于特定控制器

[英]Spring boot - NotEmpty annotation only for a specific Controller

I have a form class like this: 我有一个这样的表单类:

public class ProjectForm {
    @NotEmpty
    private String name;

    @NotEmpty
    private String desription;

    // getters and setters
}

this form is used to both create and edit Project objects which will be saved in a database. 此表单用于创建和编辑将保存在数据库中的Project对象。

I have two different controllers for creating and editing project objects. 我有两个用于创建和编辑项目对象的控制器。

@PostMapping("/projects/edit/{id}")
public String editProject(@Valid ProjectForm projectForm, BindingResult bindingResult, @PathVariable("id") String id) {
    //Controller code here
}

and

@PostMapping("/projects/new")
public String addProject(@Valid ProjectForm projectForm, BindingResult bindingResult) {
    //Controller code here
}

As you see, both controllers use the ProjectForm class. 如您所见,两个控制器都使用ProjectForm类。 How can I make the name field for the first controller mandatory and for the second one optional? 如何使第一个控制器的名称字段为必需字段,第二个控制器的名称字段为可选字段?

You named both of your methods as editProject but I assume only the first one is for editing. 您将两个方法都命名为editProject,但是我假设只有第一个方法才能进行编辑。 As a solution, you can create another class just for editing without using @NotEmpty annotation say, ProjectFormWrapper. 作为解决方案,您可以创建另一个仅用于编辑的类,而无需使用@NotEmpty批注(例如ProjectFormWrapper)。 Other than that, if you are editing an object don't use POST, use PUT. 除此之外,如果您编辑的对象不使用POST,请使用PUT。

Yes u can do it using validation-groups.see this docs validation-groups-docs 是的,您可以使用validation-groups进行此操作。请参阅此文档validation-groups-docs

public interface FirstValidation{
}


public class ProjectForm {
@NotEmpty(groups = {FirstValidation.class},message = "Name can not be empty")
private String name;

@NotEmpty
private String desription;

// getters and setters
}


@PostMapping("/projects/edit/{id}")
public String editProject(@Validated(FirstValidation.class) ProjectForm projectForm, BindingResult bindingResult, @PathVariable("id") String id) {
    //Controller code here
}

@PostMapping("/projects/new")
public String addProject(@Valid ProjectForm projectForm, BindingResult bindingResult) {
//Controller code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot Controller注解进行验证? - Spring boot controller annotation for validation? Spring中如何设置@NotEmpty注解消息? - How to set @NotEmpty annotation messages in Spring? Async on spring boot rest rest api - annotation should be on service only or controller - Async on spring boot rest rest api - annotation should be on service only or controller 带有@NotEmpty批注的Spring MVC表单变量,一个具有多个表单的POJO - Spring MVC form valiadtion with @NotEmpty annotation, one POJO with Multiple forms 是否有捷径可让基于Spring注释的验证中的每个字段@Notempty - Is there a shortcut to make every field @Notempty in Spring annotation based validation 列表不遵守@NotEmpty 注释 - List not honoring @NotEmpty annotation 导入 javax.validation.constraints.NotEmpty; 未找到 - Spring 启动 - import javax.validation.constraints.NotEmpty; not found - Spring Boot 在 spring boot 中实现 TDD,我不能在控制器中使用 @valid 注释 - implementing TDD in spring boot and i cant use @valid annotation in controller Spring Boot ConflictingBeanDefinitionException:@Controller 的注释指定的 bean 名称 class - Spring Boot ConflictingBeanDefinitionException: Annotation-specified bean name for @Controller class 将spring-boot中的过滤器应用于特定url模式的注释 - Annotation for applying filter in spring-boot to specific url patterns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM