Java Spring启动-i18n错误消息
当使用带有简单rest api的Spring boot时,由于某种原因,我得到一个错误,它是英文的,如下所示:Java Spring启动-i18n错误消息,java,spring,spring-boot,internationalization,Java,Spring,Spring Boot,Internationalization,当使用带有简单rest api的Spring boot时,由于某种原因,我得到一个错误,它是英文的,如下所示: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2018-07-35'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse \"2018-07-35\": Value 35 for dayOfMonth must be in the range [1,31]"
是否可以根据请求区域设置翻译所有这些spring启动消息?在spring中,错误日志的重要部分通常位于消息的末尾:
Value 35 for dayOfMonth must be in the range [1,31]"
public void parseDate(String string){
try{
Date date = Date.parse(string);
}catch(IllegalFieldValueException e){
System.err.println("Date " + string + "is invalid in your local");
}
}
你想创造更长的月份吗
对不起,我没有读你的问题
您不能在本地文件中转换堆栈跟踪,这意味着您需要了解本机函数和库函数的所有可能消息,然后创建一些转换文件
但是,使用try and catch,您可以捕获一些异常并打印自定义消息:
Value 35 for dayOfMonth must be in the range [1,31]"
public void parseDate(String string){
try{
Date date = Date.parse(string);
}catch(IllegalFieldValueException e){
System.err.println("Date " + string + "is invalid in your local");
}
}
我在这里使用了临时电话来做一个简单的例子
但是spring并不总是可以做到的,所以GlobalExceptionHandler是您的朋友,这里是我项目代码中的一个示例:
@ControllerAdvice
public class GlobalExceptionHandlerController {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerController.class);
@ExceptionHandler(UsernameNotFoundException.class)
public @ResponseBody
ResponseEntity usernameNotFound(UsernameNotFoundException e) {
log.info(e.getMessage());
return new ErrorResponseEntity(HttpStatus.FORBIDDEN, e);
}
@ExceptionHandler(AuthenticationException.class)
public @ResponseBody
ResponseEntity wrongAuthentication(AuthenticationException e) {
log.info(e.getMessage());
return new ErrorResponseEntity(HttpStatus.FORBIDDEN, e);
}
}
i18n与字符串到日期的转换有什么关系?无论如何,没有一个月有35天。在询问问题之前,请阅读错误原因。问题不是消息的内容。问题是关于错误消息的国际化。我希望spring boot具有某种区域设置配置,可以转换大部分消息。不,spring也使用外部库,例如,您的错误消息是由“org.joda.time.IllegalFieldValueException”生成的,与spring boot无关