简体   繁体   中英

Jackson InvalidTypeIdException when consuming JSON mapped to Hibernate Entity with other Entity references in Spring Boot Rest Controller

I am developing a REST webservice, and I have the following:

Item.java

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_generator")
    @SequenceGenerator(name="item_generator", sequenceName = "base.item_seq")
    @Column(name = "id")
    private long id;

    @Column(name = "item_name")
    private String itemName;

    @JsonIgnore
    @JsonManagedReference
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(
        name="join_category_item",
        joinColumns = @JoinColumn(name="item_id"),
        inverseJoinColumns = @JoinColumn(name="category_id")
    )
    private Category category;
}

Category.java

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Category implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_generator")
    @SequenceGenerator(name="category_generator", sequenceName = "base.category_seq")
    @Column(name = "id", nullable = false)
    private long id;

    @Column(name = "category_name")
    private String categoryName;

    @JsonBackReference
    @OneToMany(mappedBy = "category")
    @Fetch(FetchMode.JOIN)
    private Collection<Item> items;
{

Controller class:

@PostMapping(value = "/create",
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Item> createItem(@RequestBody Item item) {
   // some logic here...
}

So my problem is that when I try to call the @PostMapping from the @Controller using this json:

{
    "Item": {
        "itemName": "Item name",
        "category": {
            "categoryName": "Category Name"
        }
    }
}

I get the following error:

"[http-nio-8080-exec-6] WARN org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve type id 'categoryName' as a subtype of [simple type, class com.test.Category]: known type ids = [Category] (for POJO property 'category'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'categoryName' as a subtype of [simple type, class com.test.Category]: known type ids = [Category] (for POJO property 'category') at [Source: (PushbackInputStream); line: 5, column: 4] (through reference chain: com.test.Item["category"])]"

Also, using

{..., "Category" : {"categoryName": "Category Name"}}

in my json (Capital C), does not throw an exception, but just enters the postmapping with a null category.

Thank you in advance.

Remove the @JsonTypeInfo annotations, I don't think they are needed for the json structure that you're trying to consume. They're implying that there's an inheritance hierarchy between Item and Category which there is not, Item just has a reference to Category.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM