我在Spring控制器中有以下方法:
public Response myMethod (@PathVariable("id") String id,
@RequestParam(required = false) Map<String, Object> queryString,
@RequestHeader(value = HttpHeaders.ACCEPT, required = false) String acceptHeader,
@RequestHeader(value = HttpHeaders.CACHE_CONTROL, required = false, defaultValue = 1) String expiration,
@RequestHeader(value = HttpHeaders.FROM) String appId) {
// ...
}
在这里,我使用
@RequestHeader
提取整个Cache Control标头。是否有一种方法只能通过注入或通过专用Spring类进行进一步解析来获取max-age
部分?还是我必须手动进行?谢谢
最佳答案
您可能可以定义一个类,例如:CacheControlMaxAge
,然后是Converter<HttpRequest, CacheControlMaxAge>
,最后将参数声明为
一个CacheControlMaxAge
@Getter
@RequiredArgsConstructor
public class CacheControlMaxAge {
private final int maxAge;
}
@Component
public class CacheControlMaxAgeConverter implements Converter<HttpRequest, CacheControlMaxAge> {
public CacheControlMaxAge convert(HttpRequest request) {
// todo: extract stuff from request
return new CacheControlMaxAge(...);
}
}
public Response blah(..., CacheControlMaxAge cacheControl) { ... }