Java 如何为String.matches()生成有效的正则表达式;图像/*”;在爪哇?
在SpringBoot应用程序中,我正在制作一个API,它接受类型为application/pdf或image/*的请求头中的值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 })
@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;
}
}