调用spring数据rest存储库方法不返回链接 [英] Calling spring data rest repository method doesn't return links

查看:141
本文介绍了调用spring数据rest存储库方法不返回链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储库ClientRepository:

I have the repository "ClientRepository":

public interface ClientRepository extends PagingAndSortingRepository<Client, Long> {
}

当我请求 http:// localhost:8080 / clients / 1 然后服务器响应

When i request http://localhost:8080/clients/1 then server responds

{
  "algorithmId" : 1,
  "lastNameTxt" : "***",
  "firstNameTxt" : "**",
  "middleNameTxt" : "**",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/clients/1121495168"
    },
    "client" : {
      "href" : "http://localhost:8080/clients/1121495168"
    }
  }
}

响应有预期的链接。

当我调用存储库继承方法时另一个控制器中的findOne

When i call repository inherited method findOne in another controller

@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将它们添加到响应中?

Why doesn't response contain links in the second case? What to do to make Spring add them to response?

推荐答案

HATEOAS功能仅对Spring数据开箱即用jpa存储库用 @RepositoryRestResource 注释。这会自动公开剩余端点并添加链接。

The HATEOAS functionality is available out of the box only for the Spring data jpa repositories annotated with @RepositoryRestResource. That automatically exposes the rest endpoint and adds the links.

当您在控制器中使用存储库时,您只需获取对象,杰克逊映射器将其映射到json。

When you use the repository in the controller you just get the object and the jackson mapper maps it to json.

如果要在使用Spring MVC控制器时添加链接,请查看这里

If you want to add links when using Spring MVC controllers take a look here

这篇关于调用spring数据rest存储库方法不返回链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆