question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Q: Support for queryParam() and formParam() with default values - removed in 4.x API

See original GitHub issue

Effectively #1291 introduces a breaking API change for queryParam() and formParam() that take a default value.

Previous API was:

      String myParam = ctx.queryParam("my-param", "one");

I believe this now needs to use a Validator and needs to be written as:

      String myParam = ctx.queryParamAsClass("my-param", String.class).getOrDefault("one");

Is that correct, have I missed something?

Noting that for people like myself that prefer external validation and are not interested in using Javalin Validation this API change appears to be a step backwards. Noting that it is pretty common for queryParam and formParam to have default values and this is now incurs some extra runtime overhead along with a difference in style when a default value is desired.

Maybe I have missed something. It looks to me like the API for queryParam/formParam that takes a default value as String could be re-instated for backward compatibility given the queryParamAsClass() validator approach of 1291. What are the thoughts on that?

Thanks, Rob.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Dec 13, 2021

I’ll close this for now, but would be willing to consider xyzOrDefault methods if someone wants them enough to provide a PR.

1reaction
tipsycommented, Oct 26, 2021

Is that correct, have I missed something?

You are correct, these methods were removed on purpose. They only existed for some methods, and I thought adding this as part of validation made more sense than keeping it in the base API. If a user just wants a default value, that’s pretty easily achieved by Optional.ofNullable(ctx.queryParam("key")).orElse("value");, or ctx.queryParam("key") ?: "value" - does this need to be part of the base API?

It looks to me like the API for queryParam/formParam that takes a default value as String could be re-instated for backward compatibility given the queryParamAsClass() validator approach of 1291. What are the thoughts on that?

I’ve never been a fan of ctx.queryParam("key", "default");, I don’t think it reads well, so I would rather keep it out. I think Optional.ofNullable(ctx.queryParam("key")).orElse("value"); reads better than ctx.queryParam("key", "value").

Read more comments on GitHub >

github_iconTop Results From Across the Web

RESTEasy JAX-RS - JBoss.org
Obtain form parameters by using HttpServletRequest.getParameterMap(). Use this switch if you are calling this method within a servlet filter or eating the input ......
Read more >
AssignMessage policy | Apigee Edge
View Apigee X documentation. ... With AssignMessage policy, you typically add, change, or remove properties ... Default Value, See Default Policy tab, below....
Read more >
Chapter 13 Building RESTful Web Services with JAX-RS
This chapter describes the REST architecture, RESTful web services, and the Java API for RESTful Web Services (JAX-RS, defined in JSR 311).
Read more >
java - What are the default values for @QueryParam when ...
Short answer. The parameter values will be: valString : null; valInt : 0; valBool : false. A bit longer answer.
Read more >
Jersey 2.37 User Guide - GitHub Pages
Java API for JSON Processing (JSON-P); 9.1.4. ... Default property values for MOXy MessageBodyReader<T> ... SseEventSource subscribe() methods; 16.6.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found