Post by account_disabled on Mar 13, 2024 21:52:28 GMT -7
Than DTO objects should also be used to receive them. Otherwise it is recommended to place one parameter in the incoming parameters of the method. In this case the Controller class must be marked with the Validated annotation and the input parameters declared with a constraint annotation eg Min etc.. If the check fails a will be thrown. The sample code looks like this RequestMappingapiuser RestController Validated public class User Controller Get MappinguserId public Result MinL Long userId User DTO userDTO new UserDTO userDTO.
Set return Result okuserDTO Get MappinggetBy Account public Result getBy Account Length min Buy Email List max NotNull String account User DTO user DTO new UserDTO userDTO return Result.okuserDTO In actual development projects it is common to use unified exception handling to return the friendlier messages that we are already familiar with. Before going deeper we need to understand the relationship and difference between Valid and Validated Both Valid and Validated are used to trigger the validation process when processing a request in Spring. However there are a few key differences between the two.
Origin Valid is a standard annotation from the Java Bean known as JSR. It is not Spring specific and can be used in any Java application. On the other hand Validated is a Springspecific annotation provided by Spring itself. Function Valid is used to validate a method object or parameter in a method. It is often used when an object is received in an HTTP request and you want to check the fields of that object. Validated is used to validate method parameters on a Spring bean. It is often used when a Spring bean method has parameters that must be validated.
Set return Result okuserDTO Get MappinggetBy Account public Result getBy Account Length min Buy Email List max NotNull String account User DTO user DTO new UserDTO userDTO return Result.okuserDTO In actual development projects it is common to use unified exception handling to return the friendlier messages that we are already familiar with. Before going deeper we need to understand the relationship and difference between Valid and Validated Both Valid and Validated are used to trigger the validation process when processing a request in Spring. However there are a few key differences between the two.
Origin Valid is a standard annotation from the Java Bean known as JSR. It is not Spring specific and can be used in any Java application. On the other hand Validated is a Springspecific annotation provided by Spring itself. Function Valid is used to validate a method object or parameter in a method. It is often used when an object is received in an HTTP request and you want to check the fields of that object. Validated is used to validate method parameters on a Spring bean. It is often used when a Spring bean method has parameters that must be validated.