gpt4 book ai didi

java - 为什么我在下面的代码 "The method getHeader(String) is undefined for the type HttpRequest"中收到此错误?

转载 作者:行者123 更新时间:2023-12-02 03:08:54 29 4
gpt4 key购买 nike

我正在尝试国际化并不断收到此错误:

The method getHeader(String) is undefined for the type HttpRequest

代码:

List<Locale> LOCALES = Arrays.asList(new Locale("en"), new Locale("fr"));

public Locale resolveLocale(HttpRequest request) {
String headerLang = ((HttpRequest) request).getHeader("Accept-Language");
return headerLang == null || headerLang.isEmpty() ? Locale.getDefault()
: Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);
}

最佳答案

使用HttpServletRequest请求

HttpRequest 请求 更改为 HttpServletRequest 请求

代码

public Locale resolveLocale(HttpServletRequest request) {
String headerLang = request.getHeader("Accept-Language");
return headerLang == null || headerLang.isEmpty() ? Locale.getDefault()
: Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);
}

关于java - 为什么我在下面的代码 "The method getHeader(String) is undefined for the type HttpRequest"中收到此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57005709/

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