有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring Swagger Java日期格式验证引发异常

请帮助我解决与swagger验证相关的问题,因为同一个正则表达式在java中工作,但在swagger中失败

工作java代码:

@Test
public void test1() {
    String regex = "^((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$";
    Assert.assertTrue("Date: matched.", Pattern.matches(regex, "19881231")); -- true
}

招摇过市的合同。yml

properties:
  lineofValue:
    type: string
    pattern: ^AB|CD$
    description:  mandatory
  date:
    type: string
    pattern: ^((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$
    description:  mandatory

样本要求:

{
"lineofValue":"AB"
"date":"19881231"
}

例外情况:

{
   "code": "400",
   "status": 400,
   "message": "Validation Failed: ECMA 262 regex \"^((19|20)\\\\d\\\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$\" does not match input string \"19881231\""
}

共 (1) 个答案

  1. # 1 楼答案

    通过^{},您似乎不需要像在Java中那样转义/字符:

    properties:
      lineofValue:
        type: string
        pattern: ^AB|CD$
        description:  mandatory
      date:
        type: string
        pattern: ^((19|20)\d\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$
        description:  mandatory