java-调用spring数据其余存储库方法不会返回链接
作者:互联网
我有存储库“ ClientRepository”:
public interface ClientRepository extends PagingAndSortingRepository<Client, Long> {
}
当我请求http://localhost:8080/clients/1时,服务器响应
{
"algorithmId" : 1,
"lastNameTxt" : "***",
"firstNameTxt" : "**",
"middleNameTxt" : "**",
"_links" : {
"self" : {
"href" : "http://localhost:8080/clients/1121495168"
},
"client" : {
"href" : "http://localhost:8080/clients/1121495168"
}
}
}
响应具有预期的链接.
当我在另一个控制器中调用存储库继承的方法findOne时
@RestController
public class SearchRestController {
@Autowired
public SearchRestController(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@RequestMapping(value = "/search", method = RequestMethod.GET)
Client readAgreement(@RequestParam(value = "query") String query,
@RequestParam(value = "category") String category) {
return clientRepository.findOne(Long.parseLong(query));
}
}
它回应
{
"algorithmId" : 1,
"lastNameTxt" : "***",
"firstNameTxt" : "**",
"middleNameTxt" : "**"
}
为什么在第二种情况下响应不包含链接?如何使Spring添加他们的响应?
解决方法:
HATEOAS功能仅适用于带有@RepositoryRestResource注释的Spring数据jpa存储库.这将自动公开其余端点并添加链接.
在控制器中使用存储库时,只需获取对象,杰克逊映射器便将其映射到json.
如果您想在使用Spring MVC控制器时添加链接,请查看here
标签:java,spring,spring-data-rest,spring-hateoas 来源: https://codeday.me/bug/20191011/1895414.html