InvalidDefinitionException: "Argument #0 of constructor has no property name" #170
-
I have trouble deserializing a JSON. My DTO class has a special @Getter
public class QuestionCommentResponseDto {
private final Long id;
private final Long questionId;
private final LocalDateTime createdDate;
private final LocalDateTime modifiedDate;
private final String text;
@Setter
private AccountResponseDto owner;
public QuestionCommentResponseDto(Long id, Long questionId, LocalDateTime createdDate,
LocalDateTime modifiedDate, String text) {
this.id = id;
this.questionId = questionId;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.text = text;
}
@JsonCreator
private QuestionCommentResponseDto(Long id, Long questionId, LocalDateTime createdDate,
LocalDateTime modifiedDate, String text,
AccountResponseDto owner) {
this(id, questionId, createdDate, modifiedDate, text);
this.owner = owner;
}
// equals() and hashcode()
} // the associated DTO
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class AccountResponseDto {
private Long id;
private String username;
// equals() and hashcode() For some reason, Jackson can't use the designated constructor. The error message is obscure com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `stack.overflow.model.dto.response.QuestionCommentResponseDto`: Argument #0 of constructor [constructor for `stack.overflow.model.dto.response.QuestionCommentResponseDto` (6 args), annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)} has no property name (and is not Injectable): can not use as property-based Creator
at [Source: (String)"{"data":{"id":1,"questionId":1,"createdDate":"2023-06-03T20:16:57.238883","modifiedDate":"2023-06-03T20:16:57.238883","text":"text","owner":{"id":1,"username":"mickey_m"}}}"; line: 1, column: 1] What does it mean? What is "argument #0"? "id"? How is it problematic? It perfectly matches the field in name and type so I expect Jackson to be smart enough to map them together. And most importantly, how do I fix this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It means that unless you have registered module So: you want to make sure to include Alternatively, since you are using a framework (Lombok?), it might have means to generate annotations to indicate names, without you having to do it beyond configuration (or a single Lombok-specific annotation). Hope this helps. |
Beta Was this translation helpful? Give feedback.
It means that unless you have registered module
jackson-module-parameter-names
(see https://github.com/FasterXML/jackson-modules-java8/), names of Constructor arguments are not detected. This separation is due to historical reasons as functionality only became available on Java 8 and Jackson used to work on earlier JDKs as well.Without this module, you would need to add
@JsonProperty
next to constructor argument names:#0
refers to the first one (arguments are 0-indexed).So: you want to make sure to include
jackson-module-parameter-names
as per README at https://github.com/FasterXML/jackson-modules-java8/ (need a Maven / Gradle dependency to get it; then useregisterModule
oraddModule
).…