简体   繁体   English

调用spring数据rest存储库方法不返回链接

[英]Calling spring data rest repository method doesn't return links

I have the repository "ClientRepository": 我有存储库“ClientRepository”:

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

When i request http://localhost:8080/clients/1 then server responds 当我请求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"
    }
  }
}

Response has links as expected. 响应具有预期的链接。

When i call repository inherited method findOne in another controller 当我在另一个控制器中调用存储库继承的方法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));
    }
}

it responds 它回应

{
      "algorithmId" : 1,
      "lastNameTxt" : "***",
      "firstNameTxt" : "**",
      "middleNameTxt" : "**"
}

Why doesn't response contain links in the second case? 为什么第二种情况下响应不包含链接? What to do to make Spring add them to response? 如何让Spring将它们添加到响应中?

Why doesn't response contain links in the second case? 为什么第二种情况下响应不包含链接?

Because Spring returns what you tell it to return: a Client. 因为Spring返回你告诉它返回的内容:一个客户端。

What to do to make Spring add them to response? 如何让Spring将它们添加到响应中?

In your controller method, you have to build Resource <Client> and return it. 在您的控制器方法中,您必须构建Resource <Client>并返回它。

Based on your code, the following should get you what you want: 根据您的代码,以下内容可以满足您的需求:

@RequestMapping(value = "/search", method = RequestMethod.GET)
Client readAgreement(@RequestParam(value = "query") String query,
                     @RequestParam(value = "category") String category) {
    Client client = clientRepository.findOne(Long.parseLong(query));
    BasicLinkBuilder builder = BasicLinkBuilder.linkToCurrentMapping()
                                               .slash("clients")
                                               .slash(client.getId());
    return new Resource<>(client,
                          builder.withSelfRel(),
                          builder.withRel("client"));
}

Stepping up from this, I would also suggest you to: 加紧这个,我还建议你:

  • use /clients/search rather than /search since your search for a client (makes more sense for a RESTful service) 使用/ clients / search而不是/ search自搜索客户端以来(对RESTful服务更有意义)
  • use a RepositoryRestController, for reasons given here 使用RepositoryRestController,原因在这里给出

That should give you something like: 这应该给你这样的东西:

@RepositoryRestController
@RequestMapping("/clients")
@ResponseBody
public class SearchRestController {

    @Autowired
    private ClientRepository clientRepository;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    Client readAgreement(@RequestParam(value = "query") String query,
                         @RequestParam(value = "category") String category) {
        Client client = clientRepository.findOne(Long.parseLong(query));
        ControllerLinkBuilder builder = linkTo(SearchRestController.class).slash(client.getId());

        return new Resource<>(client,
                builder.withSelfRel(),
                builder.withRel("client"));
    }
}

The HATEOAS functionality is available out of the box only for the Spring data jpa repositories annotated with @RepositoryRestResource . 该HATEOAS功能仅适用于带注释的春天JPA的数据仓库可开箱@RepositoryRestResource That automatically exposes the rest endpoint and adds the links. 这会自动公开其余端点并添加链接。

When you use the repository in the controller you just get the object and the jackson mapper maps it to json. 当您在控制器中使用存储库时,您只需获取对象,杰克逊映射器将其映射到json。

If you want to add links when using Spring MVC controllers take a look here 如果您想在使用Spring MVC控制器时添加链接,请查看此处

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

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