小编use*_*124的帖子

注释中的Spring Boot JSR303消息代码被忽略

在我的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参数中的代码被忽略.

我不想使用默认代码.我想用自己的.我怎样才能做到这一点.能否请您提供代码示例.

bean-validation spring-boot

8
推荐指数
2
解决办法
7734
查看次数

堆栈跟踪和 golang errors.Unwrap()

我想构建一个堆栈跟踪,其中包含一个低级数据库错误和一个人类可读的第二个错误。

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)

error-handling go

7
推荐指数
1
解决办法
5295
查看次数

标签 统计

bean-validation ×1

error-handling ×1

go ×1

spring-boot ×1