Reputation: 185
I'm writing a Rest Service (HTTP Get endpoint), where in the below uri does the following
http://localhost:8080/customers/{customer_id}
Code:
@RequestMapping(method = RequestMethod.GET, value = "customers/{customer_id}")
public List<Customer> getCustomers(
@PathVariable(name = "customer_id", required = false) final String customerId) {
LOGGER.debug("customer_id {} received for getCustomers request", customerId);
}
However, with the above code, for the second scenario control is flowing to getCustomers().
Note: I'm using Java8 and spring-web 4.3.10 version
Highly appreciate any help on this.
Upvotes: 3
Views: 13015
Reputation: 630
This may help someone that is trying to use multiple optional path variables.
If you have more than one variable, you can always accept multiple paths. For instance:
@GetMapping(value = {"customers/{customerId}&{startDate}&{endDate}",
"customers/{customerId}&{startDate}&",
"customers/{customerId}&&{endDate}",
"customers/{customerId}&&"
})
public Customer getCustomerUsingFilter(@PathVariable String customerId, @PathVariable Optional<Date> startDate, @PathVariable Optional<Date> endDate)
Then you would call this URL using all the path separators (in this case &)
Like
GET /customers/1&& or
GET /customers/1&&2018-10-31T12:00:00.000+0000 or
GET /customers/1&2018-10-31T12:00:00.000+0000& or
GET /customers/1&2018-10-31T12:00:00.000+0000&2018-10-31T12:00:00.000+0000
Upvotes: 1
Reputation: 15854
You should create two end-point here to handle the individual request :
@GetMapping("/customers")
public List<Customer> getCustomers() {
LOGGER.debug("Fetching all customer");
}
@GetMapping("/customers/{id}")
public List<Customer> getCustomers(@PathVariable("id") String id) {
LOGGER.debug("Fetching customer by Id {} ",id);
}
@GetMapping
is equivalent to @RequestMapping(method = RequestMethod.GET)
and @GetMapping("/customers/{id}")
is equivalent to @RequestMapping(method = RequestMethod.GET, value = "customers/{id}")
Better approach would be like this :
@RestController
@RequestMapping("/customers")
public class CustomerController {
@GetMapping
public List<Customer> getAllCustomers() {
LOGGER.debug("Fetching all customer");
}
@GetMapping("/{id}")
public Customer getCustomerById(@PathVariable("id") String id) {
LOGGER.debug("Fetching customer by Id {} ",id);
}
Upvotes: 0
Reputation: 6946
Optional @PathVariable
is used only if you want to map both GET /customers/{customer_id}
and GET customers
into single java method.
You cannot send request which will be sent to GET /customers/{customer_id}
if you don't send customer_id
.
So in your case it will be:
@RequestMapping(method = RequestMethod.GET, value = {"/customers", "customers/{customer_id}"})
public List<Customer> getCustomers(@PathVariable(name = "customer_id", required = false) final String customerId) {
LOGGER.debug("customer_id {} received for getCustomers request", customerId);
}
public abstract boolean required
Whether the path variable is required.
Defaults to true, leading to an exception being thrown if the path variable is missing in the incoming request. Switch this to false if you prefer a null or Java 8 java.util.Optional in this case. e.g. on a ModelAttribute method which serves for different requests.
You can use null
or Optional
from java8
Upvotes: 6