我正在尝试为我的应用程序配置ResourceBundleMessageSource来翻译错误代码。
我有以下课程:
@Configuration
public class InternazionalizationConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale( new Locale("es", "ES"));
return slr;
}
@Bean
public MessageSource getMessageResource() {
ResourceBundleMessageSource messageResource= new ResourceBundleMessageSource ();
messageResource.setBasename("classpath:LocaleMessage");
messageResource.setDefaultEncoding("UTF-8");
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
}
@Component
public class Translator {
private static MessageSource messageSource;
@Autowired
Translator(MessageSource messageSource) {
Translator.messageSource = messageSource;
}
public static String translateCode(String msgCode) {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(msgCode, null, locale);
}
}
我的.properties文件位于资源文件夹中,如您在图像中所见,并且如yopu所见,属性“ signup.error.06”已定义
@ControllerAdvice
@RestController
public class SecurityExceptionHandler {
@ExceptionHandler(value = { LoginSignUpException.class })
public ResponseEntity<ExceptionDTO> handleException(LoginSignUpException e) {
String message = null;
try {
message = Translator.translateCode( e.getErrorCode() );
} catch (Exception ex){
throw ex;
}
return new ResponseEntity<>(new ExceptionDTO(e.getErrorCode(),message), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(value = { Exception.class })
public ResponseEntity<ExceptionDTO> handleException(Exception e) {
return new ResponseEntity<>(new ExceptionDTO("INTERNAL_SERVER_ERROR",e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
当我发送强制执行异常的请求时,收到以下错误:
org.springframework.context.NoSuchMessageException: No message found under code 'signup.error.06' for locale 'es_ES'.
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:76) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at com.mashosoft.backEndTest.component.Translator.translateCode(Translator.java:22) ~[classes/:na]
at com.mashosoft.backEndTest.security.exception.SecurityExceptionHandler.handleException(SecurityExceptionHandler.java:20) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
...
我尝试更改配置,因为我认为问题与找不到属性文件有关
messageResource.setBasename(“ LocaleMessage”);
我还尝试了更多操作,但没有任何变化,我仍然收到相同的错误。
谢谢