当前位置: 首页 > 知识库问答 >
问题:

如何使用@PathVariable配置spring-data-rest搜索方法路径

易阳云
2023-03-14

我想通过将参数作为路径变量传递来自定义我的spring-data-rest搜索方法路径,如下所示

http://localhost:8080/orders/search/customers/{customerId}

findByCustomer(@PathVariable("customerId") Integer customer);

搜索资源列出了以下链接

http://localhost:8080/orders/search/customers/%7BcustomerId%7D

如何使用路径参数公开搜索url?

共有2个答案

陆臻
2023-03-14

使用HttpServletRequest获取请求url:

findByCustomer(@PathVariable("customerId") Integer customer, HttpServletRequest request){
    String request = request.getRequestURL().toString(); // StringBuffer, so use append if you want to...
    [...]
}

您还可以使用request.getQueryString()获取之后的查询部分。

狄宾实
2023-03-14

您可以使用类似于以下的自定义处理程序:

@RepositoryRestController
public class OrderController {

    @Autowired
    OrderRepository orderRepository;

    @GetMapping("/orders/search/customers/{id}")
    public @ResponseBody ResponseEntity<?> getByCustomers(@PathVariable Integer customer) {
        Order order = orderRepository.findOne(id);
        if(order == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        Resource<Order> resource = new Resource<Order>(order); 
        return ResponseEntity.ok(resource);
    }
}

更多信息可在此处找到。

 类似资料:
  • 使用以下存储库: 的URL自动设置为。但是它看起来很冗长,URL可以配置为<code>/people</code>并且查询类似<code</peoples吗?name=john?

  • 我想指出pathvariable secondpart。这是不可能的,因为错误消息显示:无法确定:veranstaltung的类型。Identificationnumber,在表中:teilnehmer,用于列:[org.hibernate.mapping.Column(id)] 要使用变量secondpart,我必须更改什么?如何访问控制器方法中的变量secondpart? } }

  • 我使用的是Spring4.1.9.版本 配置下一个Rest控制器。 并创建了/get/{id}以某种方式隐藏/get/matrix/{vars}的映射 当我尝试通过下一个url调用它时http://localhost:8080/testmatrixvariables/get/matrix/;v1=1;v2=2;v3=3我从调试日志中获得 org.springframework.beans.Type

  • 我正在学习Spring Data JPA和Spring Security以及用户角色,我想知道如何使用Spring Data JPA搜索非唯一索引。例如,我有3个MySQL表,这是联接表: 我想找到一个特定用户的所有角色。所以我想在user_role表中按user_id进行搜索,这不是唯一的键。 用户实体:

  • 是否有人成功地将search\u after与spring data elasticsearch一起使用? 我已经将_uid添加到我的排序中,@Controller正确地将其放入我的Pagable中。我打开了慢速查询日志并验证了如果我手动运行@Repository生成的查询,我会得到响应: 我将以下内容添加到我的pojo(由@Repository返回的类): 排序总是返回为null。尝试了两种不同

  • 通过创建一个扩展RepositoryRestMvcConfiguration的Java配置类,并将@RestResource添加到存储库中,我将Spring Data Rest(2.0)添加到了现有的Spring MVC应用程序中。 是否可以更改Rest API的基本URL?例如: 我试图使用setBaseURI重写configureRepositoryRestConfiguration,但它似乎