S
S
smetannicPum2021-11-06 05:57:36
Java
smetannicPum, 2021-11-06 05:57:36

How to fix object parsing error in JSON?

My essence

@Entity
@Table(name = "ARTICLES",schema = "NEWS_SCHEMA")
@Getter @Setter
@NoArgsConstructor
public class Article {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "TITLE")
    private String title;

    @Column(name = "CONTENT")
    private byte[] content;
@ManyToOne
    @JoinColumn(name = "CATEGORY_ID")
    private Category categoryOfArticle;

    @ManyToOne(cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
    @JoinColumn(name = "AUTHOR_ID" )
    private Author authorOfArticle;

    public Article(String title, Category categoryOfArticle, Author authorOfArticle) {
        this.title = title;
        this.categoryOfArticle = categoryOfArticle;
        this.authorOfArticle = authorOfArticle;
    }
}

Вторая :
@Entity
@Table(name = "AUTHORS", schema = "NEWS_SCHEMA")
@Getter
@Setter
@NoArgsConstructor
public class Author {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "SURNAME")
    private String surname;

    @Column(name = "DATE_OF_BIRTH")
    private Date dob;

    @OneToMany(cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},
            mappedBy = "authorOfArticle")
    private List<Article> articlesOfAuthor;

    public void addArticleToAuthor(Article article) {

        if (articlesOfAuthor == null) {
            articlesOfAuthor = new ArrayList<>();
        }
        articlesOfAuthor.add(article);

        article.setAuthorOfArticle(this);
    }

    public Author(String name, String surname, Date dob) {
        this.name = name;
        this.surname = surname;
        this.dob = dob;
    }
}

Третья:
 @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "NAME")
    private String name;
@OneToMany(cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},
            mappedBy = "categoryOfArticle")
    private List<Article> articlesInCategory;

    public void addArticleInCategoriesList(Article article){

        if (articlesInCategory == null){
            articlesInCategory = new ArrayList<>();
        }
        articlesInCategory.add(article);
    }

    public Category(String name) {
        this.name = name;
    }
}

Метод контроллера:
@PostMapping("/articles")
    public Article addNewArticle(@RequestBody Article article) {
        articleService.saveArticle(article);

        return article;
    }

Метод контроллера обращается к сервису,который вызывает методы JpaRepository


6185ee53576e4467926003.png

Full text of the error:
WARN 5768 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
 Cannot construct instance of `com.dataart.newsportal.entities.Author` (although at least one Creator exists):
 no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/authors/7');
 nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.dataart.newsportal.entities.Author` (although at least one Creator exists):
 no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/authors/7')<LF> at [Source:
 (PushbackInputStream); line: 3, column: 20] (through reference chain: com.dataart.newsportal.entities.Article["authorOfArticle"])]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-11-06
@xez

In your model, authorOfArticle is not a string, but an Author object.
You are in a post request, trying to push a string there, about which you get an error:
Cannot construct instance of `com.dataart.newsportal.entities.Author` (although at least one Creator exists):
no String-argument constructor/factory method to deserialize from String value(' localhost:8080/authors/7 ');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question