简体   繁体   English

在Spring Boot中检索特定字段

[英]Retrieve specific fields in spring boot

I developed a restful API using Spring boot and I'm trying to find out the best approach to implement a partial response result. 我使用Spring Boot开发了一个宁静的API,并且我试图找到实现部分响应结果的最佳方法。 For now my /user/{Id} endpoint returns something like this: 现在,我的/user/{Id}端点返回如下内容:

{
    "firstname": "Jhon",
    "lastname": "Doe",
    "address": "156 Proton street",
    "username": "jhonDoe",
    "email": "jhon.doe@email.com",
    "company": "Fiction corp"
}

What I want to achieve is to expose an endpoint with a request parameter fields where I can specify the attributes that I want to retrieve, so the endpoint will be somehting like /users/{id}/fields=firstname,lastname,company and the result will be: 我要实现的是公开一个带有请求参数字段的端点,我可以在其中指定要检索的属性,因此端点将像/users/{id}/fields=firstname,lastname,company和结果将是:

{
    "firstname": "Jhon",
    "lastname": "Doe",
    "company": "Fiction corp"
}

I already made some research and found an article about Squiggle library, but they don't mention how this can be integrated with Spring boot, Also if there's any other library that doesn't have to treat only the serialization but generate a custom Query based on Spring data (repositories) to only retrieve the specified fields will be most welcomed. 我已经进行了一些研究,找到了一篇有关Squiggle库的文章,但是他们没有提及如何将其与Spring boot集成在一起。此外,如果还有其他库不必仅处理序列化,而是可以生成基于Query的自定义查询在Spring数据(存储库)上仅用于检索指定字段的方法将受到欢迎。

Is there any solutions like this? 有没有这样的解决方案? Or someone already figured out how to configure Squiggle in their existing application? 还是有人已经想出了如何在现有应用程序中配置Squiggle?

After more experiments I found the simplest way to use Squiggly library with spring boot and spring web annotations in Controller. 经过更多的实验,我发现在Controller中将Squiggly库与spring boot和spring web批注一起使用的最简单方法。 The only one thing you should do is to add configuration class as follows: 您应该做的唯一一件事是添加配置类,如下所示:

@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class SquigglyAutoconfigure {

@Bean
public FilterRegistrationBean squigglyRequestFilter(ObjectMapper objectMapper) {
        Squiggly.init(objectMapper, new RequestSquigglyContextProvider());

        FilterRegistrationBean<SquigglyRequestFilter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new SquigglyRequestFilter());
        filter.setOrder(1);
        return filter;
    }
}

And then when you add query param "fields" to any endpoint inside controller: 然后,当您将查询参数“字段”添加到控制器内的任何端点时:

@RequestParam(name = "fields", required = false) String fields

You will have filtered responses. 您将过滤出响应。 For example with this kind of response body: 例如,使用这种响应主体:

{
  "id": "ISSUE-1",
  "issueSummary": "Dragons Need Fed",
  "issueDetails": "I need my dragons fed pronto.",
  "reporter": {
      "firstName": "Daenerys",
      "lastName": "Targaryen"
  }
}

When you put a request with fields=id,reporter.firstName you will get: 当您使用fields = id,reporter.firstName发出请求时您将获得:

{
  "id": "ISSUE-1",
  "reporter": {
      "firstName": "Daenerys"
  }
}

More examples with nested objects, collections and others: https://github.com/bohnman/squiggly-java#reference-object 有关嵌套对象,集合和其他对象的更多示例: https : //github.com/bohnman/squiggly-java#reference-object

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

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