gpt4 book ai didi

java - 如何在 Spring Controller 中注入(inject) Cache-Control max-age header ?

转载 作者:行者123 更新时间:2023-12-02 09:19:01 24 4
gpt4 key购买 nike

我在 Spring Controller 中有以下方法:

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 提取整个缓存控制 header 。有没有办法仅通过注入(inject)或使用专用 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) { ... }

关于java - 如何在 Spring Controller 中注入(inject) Cache-Control max-age header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com