使用Jacksonannotation @JsonTypeInfo和@JsonSubTypes时出错

nwlls2ji  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

大家好,我得到错误如下,而从 Postman 的要求
org.springframework.core.codec.DecodingException: JSON decoding error: Could not resolve type id '4' as a subtype of com.myproject.onetoone.viewmodels.request.TopicGenerationRequest : known type ids = [GoalsGenerationRequest, TodoGenerationRequest, TopicGenerationRequest]; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '4' as a subtype of com.myproject.onetoone.viewmodels.request.TopicGenerationRequest : known type ids = [GoalsGenerationRequest, TodoGenerationRequest, TopicGenerationRequest] at [Source: (io.netty.buffer.ByteBufInputStream); line: 2, column: 13]
我的父母类:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @Type(value = GoalsGenerationRequest.class, name="GoalsGenerationRequest"),
    @Type(value = TodoGenerationRequest.class, name="TodoGenerationRequest")
})
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TopicGenerationRequest {
      private int type;
}

字符串
子类GoalsGenerationRequest:

import com.myproject.onetoone.viewmodels.request.TopicGenerationRequest;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@JsonTypeName("GoalsGenerationRequest")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class GoalsGenerationRequest extends TopicGenerationRequest {
    private List<Goal> goals;
}


子类TodoGenerationRequest:

@JsonTypeName("TodoGenerationRequest")
@Getter
@Setter
public class TodoGenerationRequest extends TopicGenerationRequest {

    @NotNull(message = "A valid UUID must be passed")
    private UUID ownerId;

    @NotBlank(message = "valid title should be added")
    private String title;

    private ZonedDateTime deadline;


}


名为Goal的GoalsGenerationRequest的子类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor

public class Goal {
    private int type;
    private int id;
}


Postman 的要求如下:

{
    "type": 4,
    "goals": [
        {
            "type": 3,
            "id": 456
        },
        {
            "type": 3,
            "id": 454
        }
        ]
}


请求被Map的端点具有以下逻辑:

Mono<ProfileIdentity> profileIdentityMono = Utility.getUserPrincipal().map(UserPrincipal::getProfileID);
        Mono<TopicGenerationRequest>  generationRequest = serverRequest.bodyToMono(TopicGenerationRequest.class)
            .onErrorResume(castError -> Mono
                .error(new ClientErrorException(ErrorCodes.INVALID_REQUEST, "please provide valid json body")));


请帮忙:)

fv2wmkja

fv2wmkja1#

当你定义了两个已知的子类型:GoalsGenerationRequestTodoGenerationRequest,所以你应该在你的JSON负载中使用,但是你设置为4,它不匹配任何已知的子类型,所以你应该设置为任何已知的类型。例如:

{
    "type": "GoalsGenerationRequest",
    "goals": [
        {
            "type": 3,
            "id": 456
        },
        {
            "type": 3,
            "id": 454
        }
        ]
}

字符串

相关问题