1

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();

}
Kamyar Miremadi
  • 169
  • 2
  • 7

2 Answers2

0

In regards to naming in a REST API: there are many different approaches and not really an accepted best practice that I'm aware of. I'd say, just pick a combination of path variables and request parameters that make sense to you and in your application.

My personal recommendation would be not to overuse path variables. The path should just refer to the resource and (optionally) the operation that you're applying on the resource, but should not include parameters to that operation. That's what request parameters are for. In your case that would be something like your second example.

If you need to build a more complex search with sorting and search in several properties, take a look at this series of tutorials. There are several articles on different approaches to this with some very good ideas. Too many to sum it up here.

Finally, keep performance and security in mind. You probably don't want to give users access to everything, and they should probably not be ably to fetch all data from a table at once (i.e. you may need pagination).

Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
0

Your 2nd version is far better. Parameters for sorting, pagination or filtering should appear as request parameters in REST request.

You should also consider using Spring Data Rest. It has out-of-the-box solutions for standard REST endpoints for your entities, which includes paging and sorting too!

The search-feature you need can be implemented in literally 2 lines under Spring Data Rest.

Selindek
  • 3,269
  • 1
  • 18
  • 25
  • Thank you so much. That's one of the thing I wanted. I am new in Java. My previous projects were all in C# .Net and GoLang. For developing an android app project now I am developing in Java and I've already loved many of the these features and frameworks in Java. – Kamyar Miremadi Jun 14 '19 at 16:05