问题
I'm writing a Rest Service (HTTP Get endpoint), where in the below uri does the following
http://localhost:8080/customers/{customer_id}
- fetch the details for the customer_id passed in the uri
- 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.
回答1:
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
回答2:
This may help someone that is trying to use multiple opcional 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 Opcional<Date> startDate, @PathVariable Opcional<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
回答3:
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);
}
来源:https://stackoverflow.com/questions/47567364/optional-pathvariable-in-rest-controller-spring-4