简体   繁体   English

使用Spring Boot和JPA,使用like语句实现查询数据库的REST服务的最佳方法是什么?

[英]What is the best way to implement a REST service that query the database using like statement using Spring Boot and JPA?

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. 我想在数据库中查询与给定字符串相似的项目(类似于sql),以后将用于我的应用程序中的自动完成功能。 I want to use REST services but I wondering what is the best approach to implement it. 我想使用REST服务,但我想知道实现它的最佳方法是什么。 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. 我也在考虑,稍后可能需要引入多个参数,例如对多个字段进行排序(asc,desc)或搜索的方式。 Here is my current solution but I am not sure if it is the best way. 这是我当前的解决方案,但是我不确定这是否是最佳方法。 My Rest service url looks something like this. 我的Rest服务网址看起来像这样。 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": 我不想在这种情况下使用RequestParam,因为它没有显示这将是“ like”运算符而不是“ 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. 寻找有关最佳REST资源命名约定以及在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();

}

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. 关于REST API中的命名:有许多不同的方法,但我所知道的并不是一种公认​​的最佳实践。 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 (ie you may need pagination). 您可能不想让用户访问所有内容,并且他们可能不应该一次就能从表中获取所有数据(即,您可能需要分页)。

Your 2nd version is far better. 您的第二版要好得多。 Parameters for sorting, pagination or filtering should appear as request parameters in REST request. 用于排序,分页或筛选的参数应在REST请求中显示为请求参数。

You should also consider using Spring Data Rest. 您还应该考虑使用Spring Data Rest。 It has out-of-the-box solutions for standard REST endpoints for your entities, which includes paging and sorting too! 它具有适用于您实体的标准REST端点的现成解决方案,其中还包括分页和排序!

The search-feature you need can be implemented in literally 2 lines under Spring Data Rest. 您需要的搜索功能可以在Spring Data Rest下的2行中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM