【问题标题】:Hal+json Model Mapping Error Unrecognized field "_embedded" Java Spring bootHal+json 模型映射错误 Unrecognized field "_embedded" Java Spring boot
【发布时间】:2021-11-27 07:49:27
【问题描述】:

我已经查看了堆栈溢出中的所有答案,以找到合适的解决方案。由于字段 _embedded,我无法反序列化对我的课程的响应。而且我有一个寻求如果我需要更多信息,我需要手动调用自我对象吗?还是有更好的方法来使用 traverson 库?

以下是我需要映射到我的班级的响应。

{
"_embedded": {
    "menu_items": [
        {
            "_embedded": {
                "menu_categories": [
                    {
                        "_links": {
                            "self": {
                                "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/categories/1001/",
                                "type": "application/hal+json; name=menu_category"
                            }
                        },
                        "id": "1001",
                        "level": 0,
                        "name": "Entree",
                        "pos_id": "1001"
                    }
                ],
                "option_sets": [
                    {
                        "_links": {
                            "modifier_group": {
                                "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/modifier_groups/1/",
                                "type": "application/hal+json; name=menu_modifier_group"
                            },
                            "self": {
                                "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/option_sets/1/",
                                "type": "application/hal+json; name=option_set"
                            }
                        },
                        "id": "1",
                        "maximum": 1,
                        "minimum": 0,
                        "required": false
                    }
                ],
                "price_levels": [
                    {
                        "_links": {
                            "self": {
                                "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/price_levels/1/",
                                "type": "application/hal+json; name=price_level"
                            }
                        },
                        "barcodes": null,
                        "id": "1",
                        "name": "Default",
                        "price_per_unit": 899
                    },
                    {
                        "_links": {
                            "self": {
                                "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/price_levels/2/",
                                "type": "application/hal+json; name=price_level"
                            }
                        },
                        "barcodes": null,
                        "id": "2",
                        "name": "Special",
                        "price_per_unit": 695
                    }
                ]
            },
            "_links": {
                "menu_categories": {
                    "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/categories/",
                    "type": "application/hal+json; name=menu_category_list"
                },
                "option_sets": {
                    "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/option_sets/",
                    "type": "application/hal+json; name=option_set_list"
                },
                "price_levels": {
                    "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/price_levels/",
                    "type": "application/hal+json; name=price_level_list"
                },
                "self": {
                    "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/101/",
                    "type": "application/hal+json; name=menu_item"
                }
            },
            "barcodes": null,
            "id": "101",
            "in_stock": null,
            "name": "Pizza",
            "open": false,
            "open_name": null,
            "pos_id": "101",
            "price_per_unit": 899
        }},
"_links": {
    "self": {
        "href": "https://api.omnivore.io/1.0/locations/T7Ez4qac/menu/items/",
        "type": "application/hal+json; name=menu_item_list"
    }
},
"count": 12,
"limit": 1000
 }

这些是模型

@Data
public class Menu {
  @JsonProperty("menu_items")
  private List<MenuItem> menuItems;
}

@Data
public class MenuItem extends RepresentationModel {

  @JsonProperty("menu_categories")
  private List<MenuCategory> menuCategory;

  @JsonProperty("option_sets")
  private List<OptionSet> optionSets;

  @JsonProperty("price_levels")
  private List<PriceLevel> priceLevels;

  @JsonProperty("barcodes")
  private String barcodes;

  @JsonProperty("id")
  private String id;

  @JsonProperty("in_stock")
  private String inStock;

  @JsonProperty("name")
  private String name;

  @JsonProperty("open")
  private Boolean open;

  @JsonProperty("open_name")
  private String openName;

  @JsonProperty("pos_id")
  private String posId;

  @JsonProperty("price_per_unit")
  private Integer pricePerUnit;

    @JsonProperty("_links")
    public void setLinks(final Map<String, Link> links) {
      links.forEach((label, link) ->  add(link.withRel(label)) );
    }
}

@Data
public class MenuCategory {
  private String id;
  private Integer level;
  private String name;
  @JsonProperty("pos_id")
  private String posId;

}

@Data
public class OptionSet  {
  private String id;
  private Integer maximum;
  private Integer minimum;
  private Boolean required;
}

@Data
public class PriceLevel {

  private String barcodes;
  private String id;
  private String name;

  @JsonProperty("price_per_unit")
  private Integer pricePerUnit;
}

下面是我用来遍历链接的代码段

 Traverson traverson = null;
    try {
      traverson =
          new Traverson(new URI("https://api.omnivore.io/1.0/locations/T7Ez4qac"), HAL_JSON);
    } catch (URISyntaxException e) {

    }
    Traverson.TraversalBuilder tb = traverson.follow("menu", "items");

    HttpHeaders headers = new HttpHeaders();
    headers.add("Api-Key", "296061b7c3144ee5b206989d13ad1d96");
    tb.withHeaders(headers);

    ObjectMapper halMapper =
        new HALMapper();
    try {
      EmbeddedWrapper value = halMapper.readValue(tb.toObject(String.class), EmbeddedWrapper.class);
      System.out.println("fsv");
    } catch (JsonProcessingException e) {

    }

    ParameterizedTypeReference<Menu> typeRefDevices =
        new ParameterizedTypeReference<Menu>() {};

    Menu embeddedWrapper = tb.toObject(typeRefDevices);

【问题讨论】:

    标签: json spring-boot traversal spring-hateoas


    【解决方案1】:

    子类MenuItem 不应扩展RepresentationModel

    我猜你对 HATEOAS 或 hal 的工作原理有些误解。

    【讨论】:

    • 我已将菜单扩展到 RepresentationModel。但 Menu 对象为空。 @yejianfengblue
    • JSON是别人生成的,不是你自己生成的吗? JSON 格式是绝对错误的。 _embedded 属性被滥用。
    • Spring HATEOAS 团队的演讲youtu.be/o4cc6KzNrpI 值得一看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2018-06-21
    相关资源
    最近更新 更多