[英]How to make a valid Regular Expression for String.matches() for “image/*” in Java?
In Spring Boot application, I am making an API which Accepts value in Request Header of type application/pdf or image/*.在 Spring 引导应用程序中,我正在制作一个 API,它接受类型为 application/pdf 或 image/* 的请求 Header 中的值。
@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)
I want to validate whether it contains any of image/* like image/png etc.我想验证它是否包含任何 image/* 像 image/png 等。
boolean acceptHeaderValid = false;
for (String acceptType : acceptTypes) {
if (acceptType.matches("image/*") || acceptType.matches(MediaType.APPLICATION_PDF_VALUE)) {
acceptHeaderValid = true;
break;
}
}
But when I send image/* in Accept header in Insomnia, acceptType.matches("image/*") evaluates to false.但是,当我在 Insomnia 中的 Accept header 中发送 image/* 时,acceptType.matches("image/*") 的计算结果为 false。 What is the mistake in the Regular Expression?
正则表达式中的错误是什么? Is there a more efficient way to do this?
有没有更有效的方法来做到这一点?
It may not be very sophisticated, but an easy approach would be to just make use of String.startsWith
Literally:它可能不是很复杂,但一种简单的方法是直接使用
String.startsWith
:
acceptType.startsWith("image/")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.