java - 具有相同模式 url 的多个路由

标签 java spring

我实际上正在创建一个简单的应用程序,我需要在多种情况下具有相同的路由模式:

/*
* Returns a list of all the root directories accepting query string on name
*/
@RequestMapping(value = "/directories", method = RequestMethod.GET)
public List<DirectoryEntity> find() {
    return directoryService.findAll();
}


/*
* Returns a list of all the root directories accepting query string on name
* @param name Name of the ressources to search. Query string at format : *name*
*/
@RequestMapping(value = "/directories", method = RequestMethod.GET)
public List<DirectoryEntity> findByCriteria(@RequestParam(value = "name", required = true) String name) {
    return directoryService.findByName(name);
}

事实上,我不想在与 findAll 相同的函数中管理条件请求。无论如何,有没有办法处理这种情况,而不必被迫管理同一函数内的所有内容?

感谢您的提前

最佳答案

尝试更改第二个方法@RequestMapping注释添加参数:

@RequestMapping(value = "/directories", method = RequestMethod.GET, params = "name")
public List<DirectoryEntity> findByCriteria(@RequestParam(value = "name", required = true) String name) {
    return directoryService.findByName(name);
}

另请参阅Spring Documentation了解更多详情。

关于java - 具有相同模式 url 的多个路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30662200/

相关文章:

java - 使用 tomcat7 部署应用程序的问题

java - 实体表不是使用 JPA 2.1 创建的

Java - 循环遍历数据库记录的有效方法

java - Swagger 文档中缺少参数端点的 Spring boot GET 和 GET

Spring 数据休息 : Date serialize date as timestamp

java - Spring Boot 1.2.5.RELEASE - 通过 Gmail SMTP 发送电子邮件

java - 为 MongoDB 淘汰 Hibernate/Mysql 或为 Java/Spring/Tomcat Web 应用程序淘汰 Couch

java - Tomcat:War 文件在本地部署但在在线服务器上获得 404

java - Netty 4.0 - 关闭无效输入的 channel

java - Google Maps API V2 崩溃 - 致命异常