Spring 其余国际化正在返回 500 http 状态

Spring rest internationalization is returning 500 http status

我正在通过 Udemy 课程学习 Spring。我已经到了话题是国际化的地步,这对我和我现在的工作都有用。 像课程一样配置代码后,当我请求 localhost:8080/hello_world_internationalized 时,我得到以下 JSON 作为响应:

{
    "timestamp": "2018-10-03T14:27:27.765+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No message found under code 'good.morning.message' for locale 'en'.",
    "path": "/hello_world_internationalized"
}

这是配置代码:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
        localeResolver.setDefaultLocale(Locale.US);
        return localeResolver;
    }

    @Bean
    public MessageSource bundleMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("app_messages");      
        return messageSource;
    }
}

public class HelloWorldController {

    @Autowired
    private MessageSource messageSource;

    @GetMapping(path = "/hello_world_internationalized")
    public String helloWorldInternationalized() {
        return messageSource.getMessage("good.morning.message", null, LocaleContextHolder.getLocale());
    }
}

我搜索了 Internet,发现一些主题讨论了 标签,其中包含国际化工作的配置。

但是我不知道把这个配置标签放在哪里。当我试图创建一个 Rest 服务时,我使用 web、rest 服务和 devtools 作为我项目的依赖项。我也在使用 HATEOAS。 我还注意到我没有配置这些东西显然需要的 WEB-INF 文件夹和 web.xml 文件。我在哪里可以创建这些文件夹和文件?

确保您创建了 app_messages_en.properties(en 语言环境的文件)和 app_messages.properties(默认语言环境的文件 ) 资源路径中的文件 (src/main/resources) 还要在此文件中添加键值

good.morning.message=value of this key

将要本地化的值的键在每个文件中都必须相同,并且值适合于它们对应的语言。

这看起来像是服务器缓存问题,无法读取属性文件。 (虽然不确定)

我已通过在 application.properties 文件中启用 DEBUG 并重新启动服务器来解决此问题。 logging.level.org.springframework=调试

它开始选择更改。