Код
ниже предназначен для извлечения всех продуктов из хранилища с использованием нумерации страниц, и после выборки данных они преобразуются в PageModel для предоставления связанных ссылок на страницы (HATEOAS) в ответе. Но когда в ответе JSON имеет такую структуру,
{
"_embedded": {
"productDToes": [
здесь productDToes генерируется автоматически. Я хочу изменить его на что-то еще, например продукты или что-то еще. полный JSON указан ниже.
ProductController. java
@GetMapping
public ResponseEntity<PagedModel<ProductDTO>> fetchAll(
@PageableDefault(direction = Direction.ASC, page = 0, size = 5, sort = { "id" }) Pageable pageable) {
ResponseEntity<PagedModel<ProductDTO>> response = this.productService.fetchAllProducts(pageable)
.filter(result -> !result.isEmpty())
.map(result -> new ResponseEntity<PagedModel<ProductDTO>>(
pageAssembler.toModel(result, productAssembler), HttpStatus.OK))
.orElse(new ResponseEntity<PagedModel<ProductDTO>>(HttpStatus.NO_CONTENT));
return response;
}
ProductDTO. java
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@NoArgsConstructor
public class ProductDTO extends RepresentationModel<ProductDTO> {
@EqualsAndHashCode.Include
@JsonIgnore
private String id;
@JsonProperty("name")
@Size(min = 2, max = 100, message = "Invalid Length..")
private String productName;
@JsonProperty("desc")
@NotBlank(message = "description is mandatory")
private String description;
private int stock;
private int price;
private List<CategoryDTO> categories;
private List<ProductPhotoDTO> photos;
public ProductDTO(String id, String productName, String description, int stock, int price,
CategoryDTO... categories) {
super();
this.id = id;
this.productName = productName;
this.description = description;
this.stock = stock;
this.price = price;
this.categories = Arrays.asList(categories);
}
}
ожидается -
{
"_embedded": {
"products": [
{
"stock": 90,
"price": 20,
"categories": [
{
"_links": {
"self": {
"href": "http://localhost:8080/organico/api/v1/categories/5e9ac74e53472c14e52dffc5"
}
},
"name": "Daily Vegetable",
"parent": "Vegetables & Fruits"
}
],
"_links": {
"self": {
"href": "http://localhost:8080/organico/api/v1/products/5e9ac74f53472c14e52dffca"
}
},
"name": "Potato",
"desc": "Daily Vegetable"
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/organico/api/v1/products?page=0&size=1&sort=id,asc"
},
"self": {
"href": "http://localhost:8080/organico/api/v1/products?page=0&size=1&sort=id,asc"
},
"next": {
"href": "http://localhost:8080/organico/api/v1/products?page=1&size=1&sort=id,asc"
},
"last": {
"href": "http://localhost:8080/organico/api/v1/products?page=14&size=1&sort=id,asc"
}
}
, но результат -
{
"_embedded": {
"productDToes": [
{
"stock": 90,
"price": 20,
"categories": [
{
"_links": {
"self": {
"href": "http://localhost:8080/organico/api/v1/categories/5e9ac74e53472c14e52dffc5"
}
},
"name": "Daily Vegetable",
"parent": "Vegetables & Fruits"
}
],
"_links": {
"self": {
"href": "http://localhost:8080/organico/api/v1/products/5e9ac74f53472c14e52dffca"
}
},
"name": "Potato",
"desc": "Daily Vegetable"
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/organico/api/v1/products?page=0&size=1&sort=id,asc"
},
"self": {
"href": "http://localhost:8080/organico/api/v1/products?page=0&size=1&sort=id,asc"
},
"next": {
"href": "http://localhost:8080/organico/api/v1/products?page=1&size=1&sort=id,asc"
},
"last": {
"href": "http://localhost:8080/organico/api/v1/products?page=14&size=1&sort=id,asc"
}
}