I want to query database for the items that are similar(sql like) to the given string which is later is going to be used for auto complete feature in my app. I want to use REST services but I wondering what is the best approach to implement it. I am also considering that might be later I need to introduce multiple parameters such as the way to sort(asc, desc) or search by multiple fields. Here is my current solution but I am not sure if it is the best way. My Rest service url looks something like this. I didn't want to use RequestParam in this case because it was not showing that this is going to be "like" operator instead of "equal":
https://localhost/rest/device/serial/like/lac
Looking for recommendations regarding best REST Resource Naming convention and ways to implement it in spring mvc.
@RestController
@RequestMapping({"rest/device"})
public class DeviceController {
private final ServiceManager serviceManager;
@GetMapping("/serial/like/{serial}")
@ResponseBody
public Iterable<Device> searchDevicesBySerialNumber(@PathVariable(value = "serial") String serial){
return this.serviceManager.getDeviceRepository().findBySerialNumberLike(serial);
}
}
This is also another way I can accomplish the same thing:
https://localhost/rest/device?serial-prefix=lac
@GetMapping("")
@ResponseBody
public Iterable<Device> searchDevice(@RequestParam(value = "serial-prefix",required = false) String serialPrefix){
if (serialPrefix!=null) {
return this.serviceManager.getDeviceRepository().findBySerialPrefix(serialPrefix);
}
return this.serviceManager.getDeviceRepository().findAll();
}