technoJ
technoJ

Reputation: 185

Optional @Pathvariable in REST controller spring 4

I'm writing a Rest Service (HTTP Get endpoint), where in the below uri does the following

http://localhost:8080/customers/{customer_id}
  1. fetch the details for the customer_id passed in the uri
  2. if the customer_id is not passed (http://localhost:8080/customers), fetch all the customers details.

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

Answers (3)

Gon&#231;alo
Gon&#231;alo

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

Mehraj Malik
Mehraj Malik

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

ByeBye
ByeBye

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

Related Questions