繁体   English   中英

如何将路径变量映射到 spring-rest 中的实体

[英]How to map a path variable to an entity in spring-rest

我在 Spring-RS 中得到了一些 REST 端点,它使用实体 ID 作为路径变量。 大多数情况下,该方法所做的第一件事是使用 id 检索实体。 有没有办法自动将 id 映射到实体,只有实体作为方法参数?

现在的情况 :

@RequestMapping(path="/{entityId})
public void method(@PathVariable String entityId) {
    Entity entity = entityRepository.findOne(entityId);
    //Do some work
}

我想要什么:

@RequestMapping(path="/{entityId})
public void method(@PathVariable Entity entityId) {
    //Do some work
}

如果您使用的是 Spring Data,那是可能的。 查看官方文档

你可以这样做:

@RequestMapping(value = "/doctor/appointments/{start}/{end}", produces = "application/json")
@ResponseBody
public List<Appointment> showAppointments(Model model, @ModelAttribute("start") Date start,
        @ModelAttribute("end") Date end, Principal principal) {

}

获取自定义对象,Spring 将尝试将参数转换为给定类型。

出于安全原因,我更喜欢在大多数情况下传递 id。

暂无
暂无

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

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