在我的Spring Boot应用程序中,我有一个支持bean,我正在使用JSR303验证.在注释中,我指定了消息代码:
@NotBlank(message = "{firstname.isnull}")
private String firstname;
Run Code Online (Sandbox Code Playgroud)
然后在我的message.properties中我指定:
firstname.isnull = Firstname cannot be empty or blank
Run Code Online (Sandbox Code Playgroud)
我对MessageSource的JavaConfig是:
@Bean(name = "messageSource")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
Run Code Online (Sandbox Code Playgroud)
验证工作正常,但我没有看到实际的字符串,而是在我的jsp页面中获取消息代码.在查看日志文件时,我看到一组代码:
Field error in object 'newAccount' on field 'firstname': rejected value []; codes [NotBlank.newAccount.firstname,NotBlank.firstname,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [newAccount.firstname,firstname]; arguments []; default message [firstname]]; default message [{firstname.isnull}]
Run Code Online (Sandbox Code Playgroud)
如果我将message.properties中的消息代码更改为数组中的某个代码,则该字符串将在我的Web表单中正确显示.我甚至不必更改注释中的代码.这向我指示注释的message参数中的代码被忽略.
我不想使用默认代码.我想用自己的.我怎样才能做到这一点.能否请您提供代码示例.
我想构建一个堆栈跟踪,其中包含一个低级数据库错误和一个人类可读的第二个错误。
errors.Unwrap()golang 1.13 中的新函数是为此目的构建的吗?不知道我明白如何使用它。寻找有关如何执行此操作的示例。
// model/book.go
package model
type Book struct {
Id uint32 `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Author string `json:"author" db:"author"`
Price float32 `json:"price" db:"price"`
}
func (b *Book) Tablename() string {
return "books"
}
// main.go
package main
func main() {
bk := model.Book{
Title: "oliver twist",
Author: "charles dickens",
Price: 10.99,
}
err:= Create(&bk)
if err !=nil {
// how to use Unwrap?
}
}
func Create(book *model.Book) error {
insertSQL := "INSERT INTO …Run Code Online (Sandbox Code Playgroud)