Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this quick tutorial, we’ll explore the differences between Spring's @RequestParam and @PathVariable annotations.

@RequestParam and @PathVariable can both be used to extract values from the request URI, but they are a bit different.

Further reading:

Validating RequestParams and PathVariables in Spring

Learn how to validate request parameters and path variables with Spring MVC

Spring's RequestBody and ResponseBody Annotations

Learn about the Spring @RequestBody and @ResponseBody annotations.

Using Spring @ResponseStatus to Set HTTP Status Code

Have a look at the @ResponseStatus annotation and how to use it to set the response status code.

2. Query Parameter vs URI Path

While @RequestParams extract values from the query string, @PathVariables extract values from the URI path:

@GetMapping("/foos/{id}")
@ResponseBody
public String getFooById(@PathVariable String id) {
    return "ID: " + id;
}

Then we can map based on the path:

http://localhost:8080/spring-mvc-basics/foos/abc
----
ID: abc

And for @RequestParam, it will be:

@GetMapping("/foos")
@ResponseBody
public String getFooByIdUsingQueryParam(@RequestParam String id) {
    return "ID: " + id;
}

which would give us the same response, just a different URI:

http://localhost:8080/spring-mvc-basics/foos?id=abc
----
ID: abc

3. Encoded vs Exact Value

Because @PathVariable is extracting values from the URI path, it’s not encoded. On the other hand, @RequestParam is encoded.

Using the previous example, ab+c will return as-is:

http://localhost:8080/spring-mvc-basics/foos/ab+c
----
ID: ab+c

But for a @RequestParam request, the parameter is URL decoded:

http://localhost:8080/spring-mvc-basics/foos?id=ab+c
----
ID: ab c

4. Optional Values

Both @RequestParam and @PathVariable can be optional.

We can make @PathVariable optional by using the required attribute starting with Spring 4.3.3:

@GetMapping({"/myfoos/optional", "/myfoos/optional/{id}"})
@ResponseBody
public String getFooByOptionalId(@PathVariable(required = false) String id){
    return "ID: " + id;
}

Then we can do either:

http://localhost:8080/spring-mvc-basics/myfoos/optional/abc
----
ID: abc

or:

http://localhost:8080/spring-mvc-basics/myfoos/optional
----
ID: null

For @RequestParam, we can also use the required attribute.

Note that we should be careful when making @PathVariable optional, to avoid conflicts in paths.

5. Conclusion

In this article, we learned the differences between @RequestParam and @PathVariable.

The full source code for the examples can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!