0

我在 Micronaut 2.2.1 中使用 Java 15 记录功能,序列化不起作用

{
    "message": "Failed to convert argument [model] for value [null] due to: Cannot construct instance of `view.model.product.ProductViewModel` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: UNKNOWN; line: -1, column: -1]",
    "path": "/model",
    "_links": {
        "self": {
            "href": "/api/v1/product",
            "templated": false
        }
    }
}

产品视图模型记录

public record ProductViewModel
        (
                @Nullable
                @JsonProperty("id")
                String id,

                @Nullable
                @JsonProperty("name")
                String name,

                @Nullable
                @JsonProperty("description")
                String description,

                @Nullable
                @JsonProperty("price")
                float price
        ) {
}

控制器

public Single<HttpResponse<?>> Create(@Body ProductViewModel model) {
        LOG.info(String.format("Controller --> Creating new product"));
        return iProductManager.Create(model).map(item -> HttpResponse.created(item));
    }
4

1 回答 1

1

我在记录中缺少@Introspected注释

@Introspected
public record ProductViewModel
        (
                @JsonProperty("id")
                String id,

                @JsonProperty("name")
                String name,

                @JsonProperty("description")
                String description,

                @JsonProperty("price")
                Float price
        ) {
}
于 2020-12-11T16:31:47.510 回答