杰克逊@JsonCreator无法识别的财产(Unrecognised Property with Jackson @JsonCreator)

编程入门 行业动态 更新时间:2024-10-22 09:51:58
杰克逊@JsonCreator无法识别的财产(Unrecognised Property with Jackson @JsonCreator)

我正在尝试使用Jackson将一些JSON转换为包含一些简单字符串和另一个类的类的实例,我正在使用@JsonCreator。 杰克逊似乎无法创建其他类的实例。

问题是,当我将此代码作为测试的一部分运行时:

ObjectMapper mapper = new ObjectMapper(); Player player = mapper.readValue(json.toString(), Player.class);

我得到以下异常:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "characterClass" (class xxx.xxx.Player), not marked as ignorable (2 known properties: "name", "character"])

我试图在我的简单测试中解析的JSON看起来像这样:

{ "name": "joe", "characterClass": "warrior", "difficulty": "easy", "timesDied": 2 }

我有一个看起来有点像这样的“玩家”

public class Player { @JsonProperty("name") private String playerName; @JsonProperty // <-- This is probably wrong private Character character; // Some getters and setters for those two fields and more }

还有另一个看起来像这样的“角色”

public class Character{ private PlayerClass playerClass; private Difficulty difficulty; private int timesDied; @JsonCreator public Character(@JsonProperty("characterClass") String playerClass, @JsonProperty("difficulty") String diff, @JsonProperty("timesDied") int died) { // Validation and conversion to enums this.playerClass = PlayerClass.WARRIOR; this.difficulty = Difficulty.EASY; this.timesDied = died; } // Again, lots of getters, setters, and other stuff }

对于像这样的小数据集,可以有更好的方法来构建整个事物,但我认为这可以用于示例的目的。 我的真实代码更复杂,但我想做一个简单的例子。

我想我已经搞砸了杰克逊的注释,但我不确定我做错了什么。

I'm trying to use Jackson to convert some JSON into an instance of a class that contains some simple Strings and another class, which I'm using @JsonCreator for. It seems that Jackson isn't able to create the instance of the other class.

The problem is that when I run this code as part of a test:

ObjectMapper mapper = new ObjectMapper(); Player player = mapper.readValue(json.toString(), Player.class);

I get the following exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "characterClass" (class xxx.xxx.Player), not marked as ignorable (2 known properties: "name", "character"])

The JSON I'm trying to parse in my simple test looks like this:

{ "name": "joe", "characterClass": "warrior", "difficulty": "easy", "timesDied": 2 }

I have a class 'Player' that looks a bit like this

public class Player { @JsonProperty("name") private String playerName; @JsonProperty // <-- This is probably wrong private Character character; // Some getters and setters for those two fields and more }

And another class 'Character' that looks like this

public class Character{ private PlayerClass playerClass; private Difficulty difficulty; private int timesDied; @JsonCreator public Character(@JsonProperty("characterClass") String playerClass, @JsonProperty("difficulty") String diff, @JsonProperty("timesDied") int died) { // Validation and conversion to enums this.playerClass = PlayerClass.WARRIOR; this.difficulty = Difficulty.EASY; this.timesDied = died; } // Again, lots of getters, setters, and other stuff }

For small sets of data like this there would be better ways to structure the whole thing, but I think this works for the purposes of an example. The real code I have is more complex but I wanted to make simple example.

I think I've messed up the Jackson annotations, but I'm not sure what I've done wrong.

最满意答案

您需要在Player上指定与JSON输入匹配的创建者。 例如:

@JsonCreator public static Player fromStringValues(@JsonProperty("name") String name, @JsonProperty("characterClass") String characterClass, @JsonProperty("difficulty") String difficulty, @JsonProperty("timesDied") Integer timesDied) { Player player = new Player(); player.setPlayerName(name); player.setCharacter(new Character(characterClass, difficulty, timesDied)); return player; }

请注意,您可以像这样构建您的枚举,杰克逊将为您执行从字符串到枚举的转换。

You need to specify a creator on Player that matches your JSON input. For example:

@JsonCreator public static Player fromStringValues(@JsonProperty("name") String name, @JsonProperty("characterClass") String characterClass, @JsonProperty("difficulty") String difficulty, @JsonProperty("timesDied") Integer timesDied) { Player player = new Player(); player.setPlayerName(name); player.setCharacter(new Character(characterClass, difficulty, timesDied)); return player; }

A side note, you can structure your enums like this and Jackson will do the conversion from string to enum for you.

更多推荐

本文发布于:2023-04-28 04:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:杰克逊   财产   无法识别   JsonCreator   Jackson

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!