简体   繁体   English

Spring Boot注解验证集自定义字段名

[英]Spring boot annotation validation set custom field name

In my spring boot rest application I'm doing validation via annotation and has the following class, how do I set a custom field name for the fields and use them in the error messages, the below example works, I can retrieve the value from the JSON payload and include them in the error message.在我的spring boot rest应用程序中,我通过注释进行验证并具有以下类,如何为字段设置自定义字段名称并在错误消息中使用它们,下面的示例有效,我可以从JSON 有效负载并将它们包含在错误消息中。

I need to set the name of the field to 'E-mail address' so I can put it in the error message.我需要将该字段的名称设置为“电子邮件地址”,以便将其放入错误消息中。

The application also does not have any forms, input is received from json.该应用程序也没有任何表单,输入是从 json 接收的。

public class Contact {
    @Size(max = 20, message = ErrorMessages.INVALID_LENGTH)
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}


public class ErrorMessages {
    public static final String INVALID_LENGTH = "'${validatedValue}' must be between {min} and {max} characters long";
}

EDIT:编辑:

JSON input JSON 输入

{
        "email": "tesaaaaaaaaaaaaaaaaaaaaaaaaaaaat@test.com"
}

expected output:预期输出:


{
    "timestamp": "2022-06-13T00:32:28.861+00:00",
    "status": 400,
    "errors": [
        "E-mail address must be between 0 and 20 characters long"
    ]
}

I don't want to hard code the field name in the error message since I want to reuse it on other fields for checking their length.我不想在错误消息中对字段名称进行硬编码,因为我想在其他字段上重用它来检查它们的长度。 I want to do something similar to ${validatedValue} but with the name instead.我想做一些类似于 ${validatedValue} 但使用名称的事情。

UPDATE:更新:

Ok seems that setting a 'label' on a field to use in interpolation is not possible, I just created a class that contains the display names that I want to use and appended the error message to it:好的,似乎不可能在字段上设置“标签”以用于插值,我刚刚创建了一个包含我要使用的显示名称的类,并将错误消息附加到它:

public class FieldName {
    public static final String E_MAIL_ADDRESS = "E-mail address";
}


public class ErrorMessages {
    private static final String INVALID_LENGTH = " '${validatedValue}' must be between {min} and {max} characters long";
    public static final String E_MAIL_INVALID_LENGTH = FieldName.E_MAIL_ADDRESS + INVALID_LENGTH;
    
}

The payload class有效载荷类

public class Contact {
    @Size(max = 20, message = ErrorMessages.E_MAIL_INVALID_LENGTH)
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Fixed output:固定输出:

{
    "timestamp": "2022-06-13T03:27:03.207+00:00",
    "status": 400,
    "errors": [
        "E-mail address 'tesaaaaaaaaaaaaaaaaaaaaaaaaaaaat@test.com' must be between 0 and 20 characters long"
    ]
}

UPDATE: Ok seems that setting a 'label' on a field to use in interpolation is not possible, I just created a class that contains the display names that I want to use and appended the error message to it:更新:好的,似乎不可能在字段上设置“标签”以用于插值,我刚刚创建了一个包含我要使用的显示名称的类并将错误消息附加到它:

public class FieldName {
    public static final String E_MAIL_ADDRESS = "E-mail address";
}


public class ErrorMessages {
    private static final String INVALID_LENGTH = " '${validatedValue}' must be between {min} and {max} characters long";
    public static final String E_MAIL_INVALID_LENGTH = FieldName.E_MAIL_ADDRESS + INVALID_LENGTH;
    
}

The payload class有效载荷类

public class Contact {
    @Size(max = 20, message = ErrorMessages.E_MAIL_INVALID_LENGTH)
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Fixed output:固定输出:

{
    "timestamp": "2022-06-13T03:27:03.207+00:00",
    "status": 400,
    "errors": [
        "E-mail address 'tesaaaaaaaaaaaaaaaaaaaaaaaaaaaat@test.com' must be between 0 and 20 characters long"
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM