【问题标题】:Spring REST API, custom entity fields in the responseSpring REST API,响应中的自定义实体字段
【发布时间】:2023-03-13 01:56:01
【问题描述】:

我正在使用 REST API(Java、SpringBoot、QueryDsl...),我想自定义结果中的字段。默认情况下,我获取实体的所有字段,但我需要的字段取决于请求。这应该是动态的。

据我所知,使用投影我可以获得类似的东西,但我必须事先声明投影,我想使用动态而不是静态的东西。另一方面,我看到 GraphQL 之类的东西允许这种行为,但我必须重构所有内容。

以前有人遇到过这个问题吗?

这是我的代码:

BaseRestCRUDController

public abstract class BaseRestCRUDController<T extends EntityBase, V extends BaseDTO> {

@GetMapping("/list")
  public ResponseEntity<List<V>> findAll(Predicate predicate, Pageable pageable) {
    log.info("FindAll");
    return new ResponseEntity(getCRUDService().findAll(predicate, pageable), HttpStatus.OK);
  }

}

示例控制器

@RestController
@RequestMapping("/api/example")
public class ExampleController
    extends BaseRestCRUDController<Example, ExampleDTO> {

  @Autowired 
  private ExampleService ExampleService;

  @Override
  public ResponseEntity<List<ExampleDTO>> findAll(
      @QuerydslPredicate(root = Example.class) Predicate predicate, Pageable pageable) {
     return super.findAll(predicate, pageable);
  }

  @Override
  protected CRUDService<Example, ExampleDTO> getCRUDService() {
    return ExampleService;
  }
  
}

示例(实体)

public class Example {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "name", nullable = false)
  private String name;

  @Column(name = "creation_date")
  private Instant creationDate;

  @Column(name = "last_update")
  private Instant lastUpdate;

  @Column(name = "erasure_date")
  private Instant erasureDate;
}
  • http://localhost:8080/api/example/list?name=test&page=0&size=5&sort=id,desc
  • http://localhost:8080/api/example/list?name=foo&page=1&size=10&sort=id,desc
  • http://localhost:8080/api/example/list?page=0&size=2&sort=id,asc
[
    {
        "id": 1,
        "name": "e1",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    },
    {
        "id": 2,
        "name": "e2",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    },
    {
        "id": 3,
        "name": "e3",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    }
]

如何在不使用投影的情况下获得这样的东西?

http://localhost:8080/api/example/list?fields=id,name&page=1&size=10&sort=id,desc

[
    {
        "id": 1,
        "name": "e1"
    },
    {
        "id": 2,
        "name": "e2"
    },
    {
        "id": 3,
        "name": "e3"
    }
]

http://localhost:8080/api/example/list?fields=name&page=1&size=10&sort=id,desc

[
    {
        "name": "e1",
    },
    {
        "name": "e2",
    },
    {
        "name": "e3",
    }
]

【问题讨论】:

  • 你用的是什么数据库?
  • 嗨! @SimonMartinelli,MySql

标签: java spring-boot querydsl


【解决方案1】:
 @Ignore
 private Instant creationDate;

试试这个。 您可以在 getter、setter 或字段上使用 @Ignore。

【讨论】:

  • @Valbs 这可能是一种解决方法,但我需要用户随时选择他们需要的字段。
  • 为此,您必须编写自己的类来映射到您的结果。正如我从请求中看到的,您可以轻松过滤掉逻辑
  • 所以这是graphql.org的情况
猜你喜欢
  • 2018-11-08
  • 2017-11-26
  • 1970-01-01
  • 2016-05-11
  • 2018-09-02
  • 2017-10-29
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多