[英]Request body validation in Spring boot application Fails
I want my REST API to throw an exception whenever there exist and parameter type mismatch.我希望我的 REST API 在存在和参数类型不匹配时抛出异常。 Following is my request object.以下是我的请求 object。
public class MyReqDto {
@NotNull
@Pattern(regexp = "[^a-zA-Z0-9]", message = "Invalid characters found in subscriberType")
private String subscriberType;
@NotNull
@Pattern(regexp = "^[a-zA-Z0-9]", message = "Invalid characters found in caseId")
private String caseId;
In above case en exception should be thrown whenever a request violate this String requirement (Providing an integer value to subscriberType).在上述情况下,只要请求违反此字符串要求(向订阅者类型提供 integer 值),就应引发异常。 Also I need to validate them against invalid characters such as *,-,<,> etc. For this I have used @Pattern annotation.我还需要针对 *、-、<、> 等无效字符验证它们。为此,我使用了 @Pattern 注释。
My controller looks like this.我的 controller 看起来像这样。
@Controller
public class MyController {
@RequestMapping(value = "/my-controller/traceId/{Trace_ID}", method = RequestMethod.POST, produces =
"application/json")
public ResponseEntity<Object> startWork(@PathVariable Optional<String> Trace_ID,
@Valid @RequestBody MyReqDto myReqDto)
throws Exception {
I am using @Valid annotation and I have included maven dependency for hibernate-validator我正在使用@Valid 注释,并且我已经包含了休眠验证器的maven 依赖项
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
But the problem is, it does not validate my input and accept integer values for subscriberType and caseId.但问题是,它不验证我的输入并接受 integer 的subscriberType 和 caseId 值。
Any help on this is highly appreciated.对此的任何帮助都将受到高度赞赏。
Not sure with acceptance criteria.不确定验收标准。 I assume you are expecting like following patterns.我假设您期望像以下模式。
Regex Patterns正则表达式模式
[A-Za-z ]*
to match letters and spaces. [A-Za-z ]*
匹配字母和空格。
".*[^0-9].*"
to match Numbers ".*[^0-9].*"
匹配数字
https://www.vogella.com/tutorials/JavaRegularExpressions/article.html https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
The issue in validating pattern was in my regex.验证模式的问题出在我的正则表达式中。 Following solved the problem.以下解决了问题。
public class MyReqDto {
@NotNull
@Pattern(regexp = "^[a-zA-Z0-9]", message = "Invalid characters found in subscriberType")
private String subscriberType;
@NotNull
@Pattern(regexp = "^[a-zA-Z0-9]", message = "Invalid characters found in caseId")
private String caseId;
I noticed that by default declaring a request body parameter as string, and providing it with a integer value works fine, no exceptions thrown.我注意到默认情况下将请求正文参数声明为字符串,并为其提供 integer 值可以正常工作,不会引发异常。 However if we declare a request body parameter as integer and provide it with a string value, it will throw an exception.但是,如果我们将请求主体参数声明为 integer 并为其提供字符串值,则会引发异常。
Simply caseId="123" and caseId=123 have same effect in my request body since caseId is declared as a string.简单的 caseId="123" 和 caseId=123 在我的请求正文中具有相同的效果,因为 caseId 被声明为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.