Jackson JSON description splitting with multiple parameter constructors

I have been using FasterXML / Jackson-Databind in my project for a while, and everything works fine until I discovered this post and started using this approach to deserialize objects without @JsonProperty annotations.

The problem is that when I have a constructor that takes several parameters and decorates this constructor with @JsonCreator annotation, Jackson throws the following error:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for com.eliti.model.Cruiser, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jack son.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator at [Source: { "class" : "com.eliti.model.Cruiser", "inventor" : "afoaisf", "type" : "MeansTransport", "capacity" : 123, "maxSpeed" : 100 }; line: 1, column: 1] 

I created a small project to illustrate the problem, the class I'm trying to execute desserialize is as follows:

 public class Cruise extends WaterVehicle { private Integer maxSpeed; @JsonCreator public Cruise(String name, Integer maxSpeed) { super(name); System.out.println("Cruise.Cruise"); this.maxSpeed = maxSpeed; } public Integer getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(Integer maxSpeed) { this.maxSpeed = maxSpeed; } } 

And the code for desserialize is as follows:

 public class Test { public static void main(String[] args) throws IOException { Cruise cruise = new Cruise("asd", 100); cruise.setMaxSpeed(100); cruise.setCapacity(123); cruise.setInventor("afoaisf"); ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); String cruiseJson = mapper.writeValueAsString(cruise); System.out.println(cruiseJson); System.out.println(mapper.readValue(cruiseJson, Cruise.class)); } 

I already tried to remove @JsonCreator, but if I do this, it will throw the following exception:

 Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.eliti.model.Cruise: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: { "class" : "com.eliti.model.Cruise", "inventor" : "afoaisf", "type" : "MeansTransport", "capacity" : 123, "maxSpeed" : 100 }; line: 3, column: 3] 

I tried issuing "mvn clean install", but the problem persists.

To include additional information, I carefully studied this issue (GitHub issues, blog posts, StackOverflow Q & A). Here are some of them: debubuging / research, which I did at the end:

Study 1

javap -v in the generated bytecode give me the following:

  MethodParameters: Name Flags name maxSpeed 

Speaking of the constructor, I assume that the -parameters flag is indeed set for the javac compiler.

Study 2

If I create a constructor with a single parameter, the object is initialized, but I need / need to use the constructor of several parameters.

Study 3

If I use the @JsonProperty annotation for each field, it works just as well, but for my original project this is too much overhead since I have many fields in the constructor (and it is also very difficult to convert the code with annotations).

The question remains: How can I get Jackson to work with several parameter constructors without annotations?

+7
java json jackson
source share
2 answers

You need to add the @JsonProperty annotation that defines the name of the json property that must be passed to the constructor when creating the object.

 public class Cruise extends WaterVehicle { private Integer maxSpeed; @JsonCreator public Cruise(@JsonProperty("name") String name, @JsonProperty("maxSpeed")Integer maxSpeed) { super(name); System.out.println("Cruise.Cruise"); this.maxSpeed = maxSpeed; } public Integer getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(Integer maxSpeed) { this.maxSpeed = maxSpeed; } } 

EDIT

I just checked using the code below and it works for me

 import java.io.IOException; import com.fasterxml.jackson.annotation.JsonCreator.Mode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; class WaterVehicle { private String name; private int capacity; private String inventor; public WaterVehicle(String name) { this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public String getInventor() { return inventor; } public void setInventor(String inventor) { this.inventor = inventor; } } class Cruise extends WaterVehicle{ private Integer maxSpeed; public Cruise(String name, Integer maxSpeed) { super(name); this.maxSpeed = maxSpeed; } public Integer getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(Integer maxSpeed) { this.maxSpeed = maxSpeed; } } public class Test { public static void main(String[] args) throws IOException { Cruise cruise = new Cruise("asd", 100); cruise.setMaxSpeed(100); cruise.setCapacity(123); cruise.setInventor("afoaisf"); ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); mapper.registerModule(new ParameterNamesModule(Mode.PROPERTIES)); String jsonString = mapper.writeValueAsString( cruise); System.out.println(jsonString); Cruise anotherCruise = mapper.readValue(jsonString, Cruise.class); System.out.println(anotherCruise ); jsonString = mapper.writeValueAsString( anotherCruise ); System.out.println(jsonString); } } 

He makes the following conclusion

 { "name" : "asd", "capacity" : 123, "inventor" : "afoaisf", "maxSpeed" : 100 } Cruise@56f4468b { "name" : "asd", "capacity" : 123, "inventor" : "afoaisf", "maxSpeed" : 100 } 

Make sure you have the compilerArgs file in the pom file.

 <compilerArgs> <arg>-parameters</arg> </compilerArgs> 
+7
source share

Short answer: use Java 8, javac -parameters and jackson-module-parameter-names

Long answer: Why, when a constructor is annotated with @JsonCreator, should its arguments be annotated with @JsonProperty?

+3
source share

All Articles