Presence of request param should evaluate to true












2















Say I have an endpoint that accepts requests as follows:



GET https://my.website.com/products?expired



OR



GET https://my.website.com/products



The method I would expect to work:



@GetMapping
public List<Product> products(@RequestParam(value = "expired", required=false) boolean expired) {
//Implementation details
}


This however, will return a Bad Request 400 response.



I know I would get this to work by sending the expired requestParam as expired=true, but I'd like for this to work similar to HTML boolean attributes where the mere presence of a request param represents true and its absence represents false










share|improve this question



























    2















    Say I have an endpoint that accepts requests as follows:



    GET https://my.website.com/products?expired



    OR



    GET https://my.website.com/products



    The method I would expect to work:



    @GetMapping
    public List<Product> products(@RequestParam(value = "expired", required=false) boolean expired) {
    //Implementation details
    }


    This however, will return a Bad Request 400 response.



    I know I would get this to work by sending the expired requestParam as expired=true, but I'd like for this to work similar to HTML boolean attributes where the mere presence of a request param represents true and its absence represents false










    share|improve this question

























      2












      2








      2








      Say I have an endpoint that accepts requests as follows:



      GET https://my.website.com/products?expired



      OR



      GET https://my.website.com/products



      The method I would expect to work:



      @GetMapping
      public List<Product> products(@RequestParam(value = "expired", required=false) boolean expired) {
      //Implementation details
      }


      This however, will return a Bad Request 400 response.



      I know I would get this to work by sending the expired requestParam as expired=true, but I'd like for this to work similar to HTML boolean attributes where the mere presence of a request param represents true and its absence represents false










      share|improve this question














      Say I have an endpoint that accepts requests as follows:



      GET https://my.website.com/products?expired



      OR



      GET https://my.website.com/products



      The method I would expect to work:



      @GetMapping
      public List<Product> products(@RequestParam(value = "expired", required=false) boolean expired) {
      //Implementation details
      }


      This however, will return a Bad Request 400 response.



      I know I would get this to work by sending the expired requestParam as expired=true, but I'd like for this to work similar to HTML boolean attributes where the mere presence of a request param represents true and its absence represents false







      java spring http spring-mvc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 20:59









      Robin-HoodieRobin-Hoodie

      3,22441648




      3,22441648
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).



          (Then just call a common method from both.)






          share|improve this answer































            4














            Use Boolean instead of boolean - the problem you have is that you are trying to unbox null value to the primitive boolean which operation causes NullpointerException and further Bad Request 400 response



            public List<Product> products(@RequestParam(value = "expired", required=false) Boolean expired)


            Here you can read something more about unboxing Boolean






            share|improve this answer



















            • 1





              Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

              – Robin-Hoodie
              Nov 21 '18 at 21:06






            • 1





              @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

              – Erwin Bolwidt
              Nov 21 '18 at 21:21






            • 1





              That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

              – Robin-Hoodie
              Nov 21 '18 at 21:22











            • Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

              – m.antkowicz
              Nov 22 '18 at 8:05













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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420405%2fpresence-of-request-param-should-evaluate-to-true%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).



            (Then just call a common method from both.)






            share|improve this answer




























              0














              I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).



              (Then just call a common method from both.)






              share|improve this answer


























                0












                0








                0







                I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).



                (Then just call a common method from both.)






                share|improve this answer













                I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).



                (Then just call a common method from both.)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 3:25









                dbreauxdbreaux

                4,21611550




                4,21611550

























                    4














                    Use Boolean instead of boolean - the problem you have is that you are trying to unbox null value to the primitive boolean which operation causes NullpointerException and further Bad Request 400 response



                    public List<Product> products(@RequestParam(value = "expired", required=false) Boolean expired)


                    Here you can read something more about unboxing Boolean






                    share|improve this answer



















                    • 1





                      Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                      – Robin-Hoodie
                      Nov 21 '18 at 21:06






                    • 1





                      @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                      – Erwin Bolwidt
                      Nov 21 '18 at 21:21






                    • 1





                      That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                      – Robin-Hoodie
                      Nov 21 '18 at 21:22











                    • Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                      – m.antkowicz
                      Nov 22 '18 at 8:05


















                    4














                    Use Boolean instead of boolean - the problem you have is that you are trying to unbox null value to the primitive boolean which operation causes NullpointerException and further Bad Request 400 response



                    public List<Product> products(@RequestParam(value = "expired", required=false) Boolean expired)


                    Here you can read something more about unboxing Boolean






                    share|improve this answer



















                    • 1





                      Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                      – Robin-Hoodie
                      Nov 21 '18 at 21:06






                    • 1





                      @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                      – Erwin Bolwidt
                      Nov 21 '18 at 21:21






                    • 1





                      That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                      – Robin-Hoodie
                      Nov 21 '18 at 21:22











                    • Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                      – m.antkowicz
                      Nov 22 '18 at 8:05
















                    4












                    4








                    4







                    Use Boolean instead of boolean - the problem you have is that you are trying to unbox null value to the primitive boolean which operation causes NullpointerException and further Bad Request 400 response



                    public List<Product> products(@RequestParam(value = "expired", required=false) Boolean expired)


                    Here you can read something more about unboxing Boolean






                    share|improve this answer













                    Use Boolean instead of boolean - the problem you have is that you are trying to unbox null value to the primitive boolean which operation causes NullpointerException and further Bad Request 400 response



                    public List<Product> products(@RequestParam(value = "expired", required=false) Boolean expired)


                    Here you can read something more about unboxing Boolean







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 21:01









                    m.antkowiczm.antkowicz

                    8,617929




                    8,617929








                    • 1





                      Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                      – Robin-Hoodie
                      Nov 21 '18 at 21:06






                    • 1





                      @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                      – Erwin Bolwidt
                      Nov 21 '18 at 21:21






                    • 1





                      That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                      – Robin-Hoodie
                      Nov 21 '18 at 21:22











                    • Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                      – m.antkowicz
                      Nov 22 '18 at 8:05
















                    • 1





                      Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                      – Robin-Hoodie
                      Nov 21 '18 at 21:06






                    • 1





                      @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                      – Erwin Bolwidt
                      Nov 21 '18 at 21:21






                    • 1





                      That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                      – Robin-Hoodie
                      Nov 21 '18 at 21:22











                    • Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                      – m.antkowicz
                      Nov 22 '18 at 8:05










                    1




                    1





                    Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                    – Robin-Hoodie
                    Nov 21 '18 at 21:06





                    Sending a GET request to https://my.website.com/products?expired will evaluate the value of the click variable to null

                    – Robin-Hoodie
                    Nov 21 '18 at 21:06




                    1




                    1





                    @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                    – Erwin Bolwidt
                    Nov 21 '18 at 21:21





                    @Robin-Hoodie That;'s right. So you need to treat null the same was false to achieve what you want.

                    – Erwin Bolwidt
                    Nov 21 '18 at 21:21




                    1




                    1





                    That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                    – Robin-Hoodie
                    Nov 21 '18 at 21:22





                    That doesn't satisfy my question though, the presence of the expired request param should be enough to have it evaluate to true

                    – Robin-Hoodie
                    Nov 21 '18 at 21:22













                    Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                    – m.antkowicz
                    Nov 22 '18 at 8:05







                    Ok I see - I believe that what you're trying to achieve is not possible in an easy way and even if you would do this it would be misleading. You should create specific endpoint like website.com/products/expired for your purpose or just keep on setting specific true/false value

                    – m.antkowicz
                    Nov 22 '18 at 8:05




















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420405%2fpresence-of-request-param-should-evaluate-to-true%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







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?