Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为String.matches()生成有效的正则表达式;图像/*”;在爪哇?_Java_Spring Boot - Fatal编程技术网

Java 如何为String.matches()生成有效的正则表达式;图像/*”;在爪哇?

Java 如何为String.matches()生成有效的正则表达式;图像/*”;在爪哇?,java,spring-boot,Java,Spring Boot,在SpringBoot应用程序中,我正在制作一个API,它接受类型为application/pdf或image/*的请求头中的值 @GetMapping(path = "/test, produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_PDF_VALUE, MediaType.IMAGE_PNG_VALUE, MediaType.APPLICATION_JSON_VALUE })

在SpringBoot应用程序中,我正在制作一个API,它接受类型为application/pdf或image/*的请求头中的值

@GetMapping(path = "/test, produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE,
        MediaType.APPLICATION_PDF_VALUE, MediaType.IMAGE_PNG_VALUE, MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> getSomething( @RequestHeader(name = "Accept") @NotEmpty List<String> 
        acceptTypes)

但当我在数据库的Accept头中发送image/*时,acceptType.matches(“image/*”)的计算结果为false。正则表达式中的错误是什么?有没有更有效的方法可以做到这一点?

这可能不是很复杂,但简单的方法是只使用
String.startsWith
字面上:

acceptType.startsWith(“image/”)
boolean acceptHeaderValid = false;
for (String acceptType : acceptTypes) {
    if (acceptType.matches("image/*") || acceptType.matches(MediaType.APPLICATION_PDF_VALUE)) {
          acceptHeaderValid = true;
          break;
    }
}