如何将模板化selfrel添加到Spring HATEOAS PagedResourcesAssembler?
我目前正在使用SpringHateOAS构建一个API。我的大多数控制器都提供了一个list方法,返回PagedResources。由于某些原因,selfrel不包含在所有示例中都可以找到的如何将模板化selfrel添加到Spring HATEOAS PagedResourcesAssembler?,spring,spring-hateoas,Spring,Spring Hateoas,我目前正在使用SpringHateOAS构建一个API。我的大多数控制器都提供了一个list方法,返回PagedResources。由于某些原因,selfrel不包含在所有示例中都可以找到的{?page,size,sort}模板。相反,我只得到基本URI 所以我的投影仪控制器看起来像 @GetMapping public PagedResources<ProjectResource> list(Pageable pageable, PagedResourcesAssembler<
{?page,size,sort}
模板。相反,我只得到基本URI
所以我的投影仪控制器看起来像
@GetMapping
public PagedResources<ProjectResource> list(Pageable pageable, PagedResourcesAssembler<Project> pagedResourcesAssembler){
Page<Project> projects = service.findAll(pageable);
return pagedResourcesAssembler.toResource(projects, assembler);
}
我想我遗漏了一些琐碎的东西,但却找不出是什么:-/在阅读了《哈尔应该如何行动》之后,似乎有一些bug 这是
任务
收集资源的标准Spring数据Rest链接
输出:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"self": {
"href": "http://localhost:8080/tasks"
},
"next": {
"href": "http://localhost:8080/tasks?page=1&size=20"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"profile": {
"href": "http://localhost:8080/profile/tasks"
},
"search": {
"href": "http://localhost:8080/tasks/search"
}
与文档中显示的相反,SpringDataREST提供的开箱即用HAL响应中没有链接被模板化
如果我遵循next
链接,则self
链接不正确:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"prev": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"self": {
"href": "http://localhost:8080/tasks"
},
"next": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"profile": {
"href": "http://localhost:8080/profile/tasks"
},
"search": {
"href": "http://localhost:8080/tasks/search"
}
}
如果我覆盖控制器:
@RequestMapping(method = RequestMethod.GET, path = "/tasks")
public ResponseEntity<Page<Task>> read(Pageable pageRequest, PersistentEntityResourceAssembler assembler) {
Page<Task> pendingTasks = taskService.read(pageRequest);
return new ResponseEntity(pageAssembler.toResource(pendingTasks, (ResourceAssembler) assembler),
HttpStatus.OK);
}
我可以通过self
链接解决这个问题。但是我们没有看到生成模板化URI的方法。以下是第二页的链接:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc"
},
"prev": {
"href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc"
},
"self": {
"href": "http://localhost:8080/tasks?page=1&size=20&sort=created,desc"
},
"next": {
"href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc"
}
希望这有助于排除可能性,但此处显示的默认行为表明,在生成收集链接时,SpringHateOAS/SpringDataREST可能存在一些问题
我使用的是
org.springframework.boot:springbootstarter数据rest:jar:1.4.0.RELEASE
,org.springframework.data:spring数据rest webmvc:jar:2.5.2.RELEASE
,org.springframework.hateoas:springhateoas:jar:0.20.0.RELEASE
在阅读了HAL在其中的行为之后,似乎有一些bug
这是任务
收集资源的标准Spring数据Rest链接
输出:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"self": {
"href": "http://localhost:8080/tasks"
},
"next": {
"href": "http://localhost:8080/tasks?page=1&size=20"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"profile": {
"href": "http://localhost:8080/profile/tasks"
},
"search": {
"href": "http://localhost:8080/tasks/search"
}
与文档中显示的相反,SpringDataREST提供的开箱即用HAL响应中没有链接被模板化
如果我遵循next
链接,则self
链接不正确:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"prev": {
"href": "http://localhost:8080/tasks?page=0&size=20"
},
"self": {
"href": "http://localhost:8080/tasks"
},
"next": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20"
},
"profile": {
"href": "http://localhost:8080/profile/tasks"
},
"search": {
"href": "http://localhost:8080/tasks/search"
}
}
如果我覆盖控制器:
@RequestMapping(method = RequestMethod.GET, path = "/tasks")
public ResponseEntity<Page<Task>> read(Pageable pageRequest, PersistentEntityResourceAssembler assembler) {
Page<Task> pendingTasks = taskService.read(pageRequest);
return new ResponseEntity(pageAssembler.toResource(pendingTasks, (ResourceAssembler) assembler),
HttpStatus.OK);
}
我可以通过self
链接解决这个问题。但是我们没有看到生成模板化URI的方法。以下是第二页的链接:
"_links": {
"first": {
"href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc"
},
"prev": {
"href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc"
},
"self": {
"href": "http://localhost:8080/tasks?page=1&size=20&sort=created,desc"
},
"next": {
"href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc"
},
"last": {
"href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc"
}
希望这有助于排除可能性,但此处显示的默认行为表明,在生成收集链接时,SpringHateOAS/SpringDataREST可能存在一些问题
我使用的是
org.springframework.boot:springbootstarter数据rest:jar:1.4.0.RELEASE
,org.springframework.data:spring数据rest webmvc:jar:2.5.2.RELEASE
,org.springframework.hateoas:spring hateoas:jar:0.20.0.RELEASE
,结果相同,selfrel中没有模板。模板在自己制作模板时会出现吗?例如,如果您访问http://localhost:8080/projects?size=2
它适用于第一个、下一个和最后一个链接。这些都是用相应的参数正确设置的。它只缺少selfref中的模板信息:-(相同的结果是,selfrel中没有模板。模板在为自己制作模板时是否显示?例如,如果您访问http://localhost:8080/projects?size=2
对于第一个、下一个和最后一个链接,它可以正常工作。这些链接使用相应的参数进行了正确设置。它只缺少selfref中的模板信息:-(