springboot2.3以上validate参数校验

1、jar

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
			

2、待校验的类

import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@Data
public class Taco {
    @NotEmpty
    @Size(min = 5,max = 20,message = "Name must be at least 5 characters long")
    private String name;

    @NotNull
    @Size(min = 1, message = "You must choose at least 1 ingredient")
    private List<String> ingredients;

}

3、控制器方法

@PostMapping
    public String procesDesign(@Valid @ModelAttribute("design") Taco design, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "design";
        }


        log.info("Process design:" + design);
        return "redirect:/orders/current";
    }

4、遇到过的问题

当时使用devtool热部署,spring-boot-starter-validation这个jar包后加入的情况下热部署项目重启jar包没生效,需要手动重启项目后才生效。

你可能感兴趣的:(spring,boot)