-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed as duplicate of#5318
Labels
3.0Issue planned for initial 3.0 releaseIssue planned for initial 3.0 release
Description
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
As discussed with @cowtowncoder in spring-projects/spring-boot#46659, there appears to be an increased need for @JsonCreator with Jackson 3.0.0-rc6 compared to 2.19.2.
Version Information
- 3.0.0-rc6
- 2.19.2
Reproduction
package com.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import tools.jackson.databind.json.JsonMapper;
public class Example {
public static void main(String[] args) throws Exception {
String json = "{\"productId\":5, \"name\":\"test\", \"weight\":42}";
JsonMapper jackson3Mapper = tools.jackson.databind.json.JsonMapper.builder().build();
System.out.println(jackson3Mapper.readValue(json, Pojo.class).getProductId());
ObjectMapper jackson2Mapper = com.fasterxml.jackson.databind.json.JsonMapper.builder().build();
jackson2Mapper.registerModule(new ParameterNamesModule());
System.out.println(jackson2Mapper.readValue(json, Pojo.class).getProductId());
}
public static class Pojo {
private final int productId;
private final String name;
private final int weight;
public Pojo() {
this.productId = 0;
this.name = null;
this.weight = 0;
}
public Pojo(int productId, String name, int weight) {
this.productId = productId;
this.name = name;
this.weight = weight;
}
public int getProductId() {
return this.productId;
}
public String getName() {
return this.name;
}
public int getWeight() {
return this.weight;
}
}
}The above should be compiled with -parameters. When run, it should produce the following output:
0
5
Jackson 3 uses the default constructor to create Pojo, Jackson 2 uses the three-argument constructor.
Expected behavior
I expected the non-default Pojo(int productId, String name, int weight) constructor to be used in both cases, and not just with Jackson 2. It is used with Jackson 3 when annotated with @JsonCreator or when the default constructor is removed.
Additional context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.0Issue planned for initial 3.0 releaseIssue planned for initial 3.0 release