Daniel Schiavo
Daniel Schiavo

Reputation: 1

Spring hateoas hal forms

As we can se on json response, there's a property named 'files' where has no properties and no options, I don't know how to proceed in this case because it is another class nested in RegisterProductRequest called AddProductFileRequest

When I faced a similar problem with a Enum (DeliveryType) I was able to add options through HalFormsConfiguration class

I want to put in property named 'files' their own properties and in their own properties their own options, how can I do it?

{
    "id": 3,
    "name": "Esse é o novo nome do prodaa",
    "description": "description",
    "price": 1200.00,
    "quantity": 3,
    "active": true,
    "sku": "321",
    "tags": [
        "tag1",
        "tag2"
    ],
    "promotion": {
        "price": 500.00,
        "start": "2000-03-03T12:00:00",
        "finish": "2025-03-03T17:00:00",
        "available": true
    },
    "files": [
        {
            "id": 4,
            "type": "IMAGE",
            "position": 0,
            "file_data": {
                "file_name": "Default.jpeg"
            }
        },
        {
            "id": 5,
            "type": "IMAGE",
            "position": 1,
            "file_data": {
                "file_name": "Default.jpeg"
            }
        }
    ],
    "nameFirstImage": "Default.jpeg",
    "bar_code": "31231",
    "category_id": 1,
    "package_info": {
        "format": "ROLL",
        "height": 2.0,
        "width": 20.5,
        "length": 5.0,
        "weight": 24.0
    },
    "deliveries_types": [],
    "_links": {
        "self": {
            "href": "http://localhost:8080/public/products/3"
        },
        "all_products": {
            "href": "http://localhost:8080/public/products"
        }
    },
    "_templates": {
        "default": {
            "method": "POST",
            "properties": [
                {
                    "name": "active",
                    "required": true
                },
                {
                    "name": "bar_code",
                    "type": "text"
                },
                {
                    "name": "category_id",
                    "required": true,
                    "type": "number"
                },
                {
                    "name": "delivery_types",
                    "required": true,
                    "min": 1,
                    "max": 2,
                    "type": "checkbox",
                    "options": {
                        "inline": [
                            "CORREIOS_SEDEX",
                            "CORREIOS_PAC",
                            "EXPRESS",
                            "PICK_UP_IN_STORE",
                            "DIGITAL"
                        ]
                    }
                },
                {
                    "name": "description",
                    "regex": "^(?=\\s*\\S).*$",
                    "required": true,
                    "type": "textarea"
                },
                {
                    "name": "files",
                    "required": true
                },
                {
                    "name": "name",
                    "regex": "^(?=\\s*\\S).*$",
                    "required": true,
                    "type": "text"
                },
                {
                    "name": "package_info"
                },
                {
                    "name": "price",
                    "required": true,
                    "type": "number"
                },
                {
                    "name": "promotion"
                },
                {
                    "name": "quantity",
                    "required": true,
                    "type": "number"
                },
                {
                    "name": "sku",
                    "type": "text"
                },
                {
                    "name": "tags"
                }
            ],
            "target": "http://localhost:8080/admin/products"
        }
    }
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RegisterProductRequest {

    @NotBlank
    private String name;

    @NotBlank
    @InputType(InputTypeHF.TEXTAREA)
    private String description;

    @NotNull
    @Positive
    private BigDecimal price;

    @NotNull
    private Integer quantity;

    @NotNull
    private Boolean active;

    @JsonProperty("bar_code")
    private String barCode;

    private String sku;

    private String[] tags;

    private ProductPromotionRequest promotion;

    @JsonProperty("package_info")
    private PackageInfo packageInfo;

    @NotNull
    @JsonProperty("category_id")
    private Long categoryId;

    @NotNull
    @JsonProperty("delivery_types")
    @Size(max = 2, min = 1)
    @InputType(InputTypeHF.CHECKBOX)
    private Set<DeliveryType> deliveryTypes;

    @NotNull
    private Set<AddProductFileRequest> files;

}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AddProductFileRequest {

        private ProductFileType type;
        @JsonProperty("file_name")
        private String fileName;
        @JsonProperty("url_video")
        private String urlVideo;
        @NotNull
        private Byte position;

}
public enum ProductFileType {
    IMAGE,
    VIDEO
}

@Configuration
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL_FORMS)
public class HateoasConfiguration {

    @Bean
    HalFormsConfiguration halFormsConfiguration() {
        return new HalFormsConfiguration()
                .customize(new ObjectMapper())
                .withOptions(RegisterProductRequest.class, "delivery_types", metadata ->
                    HalFormsOptions.inline(DeliveryType.values()));
    }


}

What do I expect:

{
    "id": 3,
    "name": "Product Name",
    ...
    "files": [
        {
            "type": "IMAGE",
            "file_name": "Default.jpeg",
            "url_video": "http://example.com/video",
            "position": 0
        },
        {
            "type": "VIDEO",
            "file_name": "Video.mp4",
            "url_video": "http://example.com/video",
            "position": 1
        }
    ],
    ...
    "_templates": {
        "default": {
            "method": "POST",
            "properties": [
                ...
                {
                    "name": "files",
                    "required": true,
                    "properties": [
                        {
                            "name": "type",
                            "required": true,
                            "type": "select",
                            "options": {
                                "inline": [
                                    "IMAGE",
                                    "VIDEO",
                                    "DOCUMENT"
                                ]
                            }
                        },
                        {
                            "name": "file_name",
                            "required": true,
                            "type": "text"
                        },
                        {
                            "name": "url_video",
                            "type": "text"
                        },
                        {
                            "name": "position",
                            "required": true,
                            "type": "number"
                        }
                    ]
                }
                ...
            ]
        }
    }
}

Upvotes: 0

Views: 88

Answers (0)

Related Questions