JSR-303 Annotating Field vs Getter
When using JSR-303 annotations to perform bean validation, what is the difference between annotating the field versus the getter?
Is one approach recommended over the other?
Annotation on field
public class Person
@NotBlank
private String firstName;
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
Annotation on getter
public class Person
private String firstName;
@NotBlank
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
java annotations bean-validation
add a comment |
When using JSR-303 annotations to perform bean validation, what is the difference between annotating the field versus the getter?
Is one approach recommended over the other?
Annotation on field
public class Person
@NotBlank
private String firstName;
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
Annotation on getter
public class Person
private String firstName;
@NotBlank
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
java annotations bean-validation
add a comment |
When using JSR-303 annotations to perform bean validation, what is the difference between annotating the field versus the getter?
Is one approach recommended over the other?
Annotation on field
public class Person
@NotBlank
private String firstName;
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
Annotation on getter
public class Person
private String firstName;
@NotBlank
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
java annotations bean-validation
When using JSR-303 annotations to perform bean validation, what is the difference between annotating the field versus the getter?
Is one approach recommended over the other?
Annotation on field
public class Person
@NotBlank
private String firstName;
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
Annotation on getter
public class Person
private String firstName;
@NotBlank
public String getFirstName()
return firstName;
public String setFirstName(String firstName)
this.firstName = firstName;
java annotations bean-validation
java annotations bean-validation
asked Jan 10 '18 at 4:13
kernelpanickernelpanic
3881520
3881520
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.
The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.
Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48180197%2fjsr-303-annotating-field-vs-getter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.
The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.
Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
add a comment |
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.
The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.
Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
add a comment |
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.
The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.
Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1, “Constraint annotation”), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
When a constraint is defined on a class, the class instance being validated is passed to the ConstraintValidator. When a constraint is defined on a field, the value of the field is passed to the ConstraintValidator. When a constraint is defined on a getter, the result of the getter invocation is passed to the ConstraintValidator.
The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.
Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.
edited Nov 15 '18 at 4:02
answered Jan 10 '18 at 4:32
VPKVPK
2,54811931
2,54811931
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
add a comment |
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
2
2
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
Your link to a question is broken
– GabrielOshiro
Nov 13 '18 at 16:44
1
1
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
@GabrielOshiro, thank you! I The important contents of the link was added to the answer to make sure the solution available, now I have removed the link.
– VPK
Nov 15 '18 at 4:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48180197%2fjsr-303-annotating-field-vs-getter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown