Presence of request param should evaluate to true
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
add a comment |
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
add a comment |
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
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
java spring http spring-mvc
asked Nov 21 '18 at 20:59
Robin-HoodieRobin-Hoodie
3,22441648
3,22441648
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.)
add a comment |
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
1
Sending aGET
request tohttps://my.website.com/products?expired
will evaluate the value of theclick
variable tonull
– Robin-Hoodie
Nov 21 '18 at 21:06
1
@Robin-Hoodie That;'s right. So you need to treatnull
the same wasfalse
to achieve what you want.
– Erwin Bolwidt
Nov 21 '18 at 21:21
1
That doesn't satisfy my question though, the presence of theexpired
request param should be enough to have it evaluate totrue
– 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 likewebsite.com/products/expired
for your purpose or just keep on setting specifictrue/false
value
– m.antkowicz
Nov 22 '18 at 8: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%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
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.)
add a comment |
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.)
add a comment |
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.)
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.)
answered Nov 22 '18 at 3:25
dbreauxdbreaux
4,21611550
4,21611550
add a comment |
add a comment |
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
1
Sending aGET
request tohttps://my.website.com/products?expired
will evaluate the value of theclick
variable tonull
– Robin-Hoodie
Nov 21 '18 at 21:06
1
@Robin-Hoodie That;'s right. So you need to treatnull
the same wasfalse
to achieve what you want.
– Erwin Bolwidt
Nov 21 '18 at 21:21
1
That doesn't satisfy my question though, the presence of theexpired
request param should be enough to have it evaluate totrue
– 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 likewebsite.com/products/expired
for your purpose or just keep on setting specifictrue/false
value
– m.antkowicz
Nov 22 '18 at 8:05
add a comment |
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
1
Sending aGET
request tohttps://my.website.com/products?expired
will evaluate the value of theclick
variable tonull
– Robin-Hoodie
Nov 21 '18 at 21:06
1
@Robin-Hoodie That;'s right. So you need to treatnull
the same wasfalse
to achieve what you want.
– Erwin Bolwidt
Nov 21 '18 at 21:21
1
That doesn't satisfy my question though, the presence of theexpired
request param should be enough to have it evaluate totrue
– 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 likewebsite.com/products/expired
for your purpose or just keep on setting specifictrue/false
value
– m.antkowicz
Nov 22 '18 at 8:05
add a comment |
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
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
answered Nov 21 '18 at 21:01
m.antkowiczm.antkowicz
8,617929
8,617929
1
Sending aGET
request tohttps://my.website.com/products?expired
will evaluate the value of theclick
variable tonull
– Robin-Hoodie
Nov 21 '18 at 21:06
1
@Robin-Hoodie That;'s right. So you need to treatnull
the same wasfalse
to achieve what you want.
– Erwin Bolwidt
Nov 21 '18 at 21:21
1
That doesn't satisfy my question though, the presence of theexpired
request param should be enough to have it evaluate totrue
– 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 likewebsite.com/products/expired
for your purpose or just keep on setting specifictrue/false
value
– m.antkowicz
Nov 22 '18 at 8:05
add a comment |
1
Sending aGET
request tohttps://my.website.com/products?expired
will evaluate the value of theclick
variable tonull
– Robin-Hoodie
Nov 21 '18 at 21:06
1
@Robin-Hoodie That;'s right. So you need to treatnull
the same wasfalse
to achieve what you want.
– Erwin Bolwidt
Nov 21 '18 at 21:21
1
That doesn't satisfy my question though, the presence of theexpired
request param should be enough to have it evaluate totrue
– 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 likewebsite.com/products/expired
for your purpose or just keep on setting specifictrue/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
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%2f53420405%2fpresence-of-request-param-should-evaluate-to-true%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