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,




  1. is this the right way to implement it?

  2. is the RuntimeException the correct choice?

  3. 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;
}
}
}









share|improve this question




























    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,




    1. is this the right way to implement it?

    2. is the RuntimeException the correct choice?

    3. 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;
    }
    }
    }









    share|improve this question


























      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,




      1. is this the right way to implement it?

      2. is the RuntimeException the correct choice?

      3. 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;
      }
      }
      }









      share|improve this question















      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,




      1. is this the right way to implement it?

      2. is the RuntimeException the correct choice?

      3. 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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 10:36









      rsp

      20k44558




      20k44558










      asked Nov 7 at 8:24









      Robert

      112




      112
























          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.






          share|improve this answer























          • 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











          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',
          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
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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.






          share|improve this answer























          • 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















          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.






          share|improve this answer























          • 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













          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 the ConfigurationProperties explicitly via EnableConfigurationProperties(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










          • @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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()