Daniel Schiavo
Daniel Schiavo

Reputation: 1

Spring Hateoas hal forms how to order property

I want to order the properties of my template so that it follows the order of the pojo attributes, the spring hateoas standard I see now that it is ordered alphabetically, that's all I want

I tried to disable the alphabetical sorting of the objectmapper in halfformsconfiguration but it didn't work

I tried to use @JsonPropertyOrder and doesn't worked as well

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonPropertyOrder({
        "name",
        "description",
        "price",
        "quantity",
        "active",
        "bar_code",
        "sku",
        "tags",
        "promotion",
        "package_info",
        "category_id",
        "delivery_types",
        "files"
})
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;

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

    @Bean
    HalFormsConfiguration halFormsConfiguration() {
        return new HalFormsConfiguration()
                .withObjectMapperCustomizer(objectMapper -> {objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false);})
                .withOptions(RegisterProductRequest.class, "delivery_types", metadata ->
                    HalFormsOptions.inline(DeliveryType.values()));
    }


}
{
    "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": "PUT",
            "properties": [
                {
                    "name": "active"
                },
                {
                    "name": "bar_code",
                    "type": "text"
                },
                {
                    "name": "category_id",
                    "type": "number"
                },
                {
                    "name": "delivery_types"
                },
                {
                    "name": "description",
                    "type": "text"
                },
                {
                    "name": "files"
                },
                {
                    "name": "name",
                    "type": "text"
                },
                {
                    "name": "package_info"
                },
                {
                    "name": "price",
                    "type": "number"
                },
                {
                    "name": "promotion"
                },
                {
                    "name": "quantity",
                    "type": "number"
                },
                {
                    "name": "sku",
                    "type": "text"
                },
                {
                    "name": "tags"
                }
            ],
            "target": "http://localhost:8080/admin/products/3"
        },
        "registerProduct": {
            "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"
        }
    }
}

Upvotes: 0

Views: 87

Answers (0)

Related Questions