[英]Spring REST API, custom entity fields in the response
I'm working in a REST API (Java, SpringBoot, QueryDsl...) and I would like to customize the fields that I have in the result.我正在使用 REST API(Java、SpringBoot、QueryDsl...),我想自定义结果中的字段。 By default I'm obtaining all fields of the entity but the fields that I need depends on the request.默认情况下,我获取实体的所有字段,但我需要的字段取决于请求。 This should be something dynamic.这应该是动态的。
As far as I know, using projections I can obtain something like this but I have to declare previously the projection and I would like to work with something dynamic not static.据我所知,使用投影我可以获得这样的东西,但我必须事先声明投影,我想使用动态而不是静态的东西。 On the other hand I have seen than something like GraphQL allows this behaviour but I would have to refactor everything.另一方面,我已经看到 GraphQL 之类的东西允许这种行为,但我必须重构一切。
Has anyone had this problem before?以前有人遇到过这个问题吗?
This is my code:这是我的代码:
BaseRestCRUDController BaseRestCRUD控制器
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);
}
}
ExampleController示例控制器
@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;
}
}
Example (Entity)示例(实体)
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;
}
[
{
"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
}
]
How can I obtain something like this without use projections?如何在不使用投影的情况下获得这样的东西?
http://localhost:8080/api/example/list?fields=id,name&name=foo&page=1&size=10&sort=id,desc http://localhost:8080/api/example/list?fields=id,name&name=foo&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&name=foo&page=1&size=10&sort=id,desc http://localhost:8080/api/example/list?fields=name&name=foo&page=1&size=10&sort=id,desc
[
{
"name": "e1",
},
{
"name": "e2",
},
{
"name": "e3",
}
]
@Ignore
private Instant creationDate;
Try this.尝试这个。 You can use @Ignore on getter,setter or fields.您可以在 getter、setter 或字段上使用 @Ignore。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.