Spring Boot start the application with configurations check
up vote
2
down vote
favorite
It is about a Spring Boot
application. I would like to check and inform the user at the beginning if someone configured false the Yaml file
. I would like to ask some of you,
- is this the right way to implement it?
- is the
RuntimeException
the correct choice? - did I use the
lombok
annotation correctly?
Thank you
@Component
@ConfigurationProperties
@Data
@NoArgsConstructor
public class ApplicationProperties{
@Data
@NoArgsConstructor
public static class Something {
private String name;
@Setter(AccessLevel.NONE)
private int width;
@Setter(AccessLevel.NONE)
private int height;
public void setWidth(int width) throws Throwable {
if (0 > width || 100 < width) {
throw new RuntimeException("The width should be between 0 and 100.");
}
this.width = width;
}
public void setHeight(int height) throws Throwable {
if (0 > height || 250 < height) {
throw new RuntimeException("The height should be between 0 and 250.");
}
this.height = height;
}
}
}
spring-boot lombok
add a comment |
up vote
2
down vote
favorite
It is about a Spring Boot
application. I would like to check and inform the user at the beginning if someone configured false the Yaml file
. I would like to ask some of you,
- is this the right way to implement it?
- is the
RuntimeException
the correct choice? - did I use the
lombok
annotation correctly?
Thank you
@Component
@ConfigurationProperties
@Data
@NoArgsConstructor
public class ApplicationProperties{
@Data
@NoArgsConstructor
public static class Something {
private String name;
@Setter(AccessLevel.NONE)
private int width;
@Setter(AccessLevel.NONE)
private int height;
public void setWidth(int width) throws Throwable {
if (0 > width || 100 < width) {
throw new RuntimeException("The width should be between 0 and 100.");
}
this.width = width;
}
public void setHeight(int height) throws Throwable {
if (0 > height || 250 < height) {
throw new RuntimeException("The height should be between 0 and 250.");
}
this.height = height;
}
}
}
spring-boot lombok
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
It is about a Spring Boot
application. I would like to check and inform the user at the beginning if someone configured false the Yaml file
. I would like to ask some of you,
- is this the right way to implement it?
- is the
RuntimeException
the correct choice? - did I use the
lombok
annotation correctly?
Thank you
@Component
@ConfigurationProperties
@Data
@NoArgsConstructor
public class ApplicationProperties{
@Data
@NoArgsConstructor
public static class Something {
private String name;
@Setter(AccessLevel.NONE)
private int width;
@Setter(AccessLevel.NONE)
private int height;
public void setWidth(int width) throws Throwable {
if (0 > width || 100 < width) {
throw new RuntimeException("The width should be between 0 and 100.");
}
this.width = width;
}
public void setHeight(int height) throws Throwable {
if (0 > height || 250 < height) {
throw new RuntimeException("The height should be between 0 and 250.");
}
this.height = height;
}
}
}
spring-boot lombok
It is about a Spring Boot
application. I would like to check and inform the user at the beginning if someone configured false the Yaml file
. I would like to ask some of you,
- is this the right way to implement it?
- is the
RuntimeException
the correct choice? - did I use the
lombok
annotation correctly?
Thank you
@Component
@ConfigurationProperties
@Data
@NoArgsConstructor
public class ApplicationProperties{
@Data
@NoArgsConstructor
public static class Something {
private String name;
@Setter(AccessLevel.NONE)
private int width;
@Setter(AccessLevel.NONE)
private int height;
public void setWidth(int width) throws Throwable {
if (0 > width || 100 < width) {
throw new RuntimeException("The width should be between 0 and 100.");
}
this.width = width;
}
public void setHeight(int height) throws Throwable {
if (0 > height || 250 < height) {
throw new RuntimeException("The height should be between 0 and 250.");
}
this.height = height;
}
}
}
spring-boot lombok
spring-boot lombok
edited Nov 7 at 10:36
rsp
20k44558
20k44558
asked Nov 7 at 8:24
Robert
112
112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
First of all, welcome to SO! Let me explain how you can validate the application properties. There is an easy way how to achieve this using the validation annotations. The @ConfigurationProperties
support JSR-303 bean validation:
@ConfigurationProperties("prefix")
public class MyProperties {
@Max(100)
@Min(0)
private Integer width;
@Max(100)
@Min(1)
private Integer height;
}
Please, note, that this approach will fail the application start if the validation throws an exception.
Secondly, to understand whether this is the right way to do it, you would have to describe your use case. I would personally stick to existing standards and use the validation annotations flow.
Lastly, as for your lombok
annotations. You can use @Getter
and @Setter
annotations either globally or place them like this on your fields to specify fine-grained access.
I am not a big fan of the @Data
annotation since it might generate some extra methods that you might not (or might, again, depends on your usage) want. I remember having some problems with nested entities and cyclic dependencies with the generated toString
method.
You can also drop the@Configuration
if you enable theConfigurationProperties
explicitly viaEnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
First of all, welcome to SO! Let me explain how you can validate the application properties. There is an easy way how to achieve this using the validation annotations. The @ConfigurationProperties
support JSR-303 bean validation:
@ConfigurationProperties("prefix")
public class MyProperties {
@Max(100)
@Min(0)
private Integer width;
@Max(100)
@Min(1)
private Integer height;
}
Please, note, that this approach will fail the application start if the validation throws an exception.
Secondly, to understand whether this is the right way to do it, you would have to describe your use case. I would personally stick to existing standards and use the validation annotations flow.
Lastly, as for your lombok
annotations. You can use @Getter
and @Setter
annotations either globally or place them like this on your fields to specify fine-grained access.
I am not a big fan of the @Data
annotation since it might generate some extra methods that you might not (or might, again, depends on your usage) want. I remember having some problems with nested entities and cyclic dependencies with the generated toString
method.
You can also drop the@Configuration
if you enable theConfigurationProperties
explicitly viaEnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
add a comment |
up vote
3
down vote
First of all, welcome to SO! Let me explain how you can validate the application properties. There is an easy way how to achieve this using the validation annotations. The @ConfigurationProperties
support JSR-303 bean validation:
@ConfigurationProperties("prefix")
public class MyProperties {
@Max(100)
@Min(0)
private Integer width;
@Max(100)
@Min(1)
private Integer height;
}
Please, note, that this approach will fail the application start if the validation throws an exception.
Secondly, to understand whether this is the right way to do it, you would have to describe your use case. I would personally stick to existing standards and use the validation annotations flow.
Lastly, as for your lombok
annotations. You can use @Getter
and @Setter
annotations either globally or place them like this on your fields to specify fine-grained access.
I am not a big fan of the @Data
annotation since it might generate some extra methods that you might not (or might, again, depends on your usage) want. I remember having some problems with nested entities and cyclic dependencies with the generated toString
method.
You can also drop the@Configuration
if you enable theConfigurationProperties
explicitly viaEnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
add a comment |
up vote
3
down vote
up vote
3
down vote
First of all, welcome to SO! Let me explain how you can validate the application properties. There is an easy way how to achieve this using the validation annotations. The @ConfigurationProperties
support JSR-303 bean validation:
@ConfigurationProperties("prefix")
public class MyProperties {
@Max(100)
@Min(0)
private Integer width;
@Max(100)
@Min(1)
private Integer height;
}
Please, note, that this approach will fail the application start if the validation throws an exception.
Secondly, to understand whether this is the right way to do it, you would have to describe your use case. I would personally stick to existing standards and use the validation annotations flow.
Lastly, as for your lombok
annotations. You can use @Getter
and @Setter
annotations either globally or place them like this on your fields to specify fine-grained access.
I am not a big fan of the @Data
annotation since it might generate some extra methods that you might not (or might, again, depends on your usage) want. I remember having some problems with nested entities and cyclic dependencies with the generated toString
method.
First of all, welcome to SO! Let me explain how you can validate the application properties. There is an easy way how to achieve this using the validation annotations. The @ConfigurationProperties
support JSR-303 bean validation:
@ConfigurationProperties("prefix")
public class MyProperties {
@Max(100)
@Min(0)
private Integer width;
@Max(100)
@Min(1)
private Integer height;
}
Please, note, that this approach will fail the application start if the validation throws an exception.
Secondly, to understand whether this is the right way to do it, you would have to describe your use case. I would personally stick to existing standards and use the validation annotations flow.
Lastly, as for your lombok
annotations. You can use @Getter
and @Setter
annotations either globally or place them like this on your fields to specify fine-grained access.
I am not a big fan of the @Data
annotation since it might generate some extra methods that you might not (or might, again, depends on your usage) want. I remember having some problems with nested entities and cyclic dependencies with the generated toString
method.
edited Nov 7 at 10:47
answered Nov 7 at 8:58
Smajl
2,6951662118
2,6951662118
You can also drop the@Configuration
if you enable theConfigurationProperties
explicitly viaEnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
add a comment |
You can also drop the@Configuration
if you enable theConfigurationProperties
explicitly viaEnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
You can also drop the
@Configuration
if you enable the ConfigurationProperties
explicitly via EnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
You can also drop the
@Configuration
if you enable the ConfigurationProperties
explicitly via EnableConfigurationProperties(MyProperties.class)
– Darren Forsythe
Nov 7 at 10:43
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
@DarrenForsythe Good point, updated.
– Smajl
Nov 7 at 10:46
add a comment |
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%2f53185722%2fspring-boot-start-the-application-with-configurations-check%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