@smetannicPum

Как исправить ошибку парсинга объекта в JSON?

Моя сущность

@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

Полный текст ошибки:
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"])]
  • Вопрос задан
  • 5173 просмотра
Пригласить эксперта
Ответы на вопрос 1
xez
@xez Куратор тега Java
TL Junior Roo
У вас в модели authorOfArticle - это не строка, а объект Author.
Вы же в пост запросе, пытаетесь туда строку впихнуть, о чем вам и выдается ошибка:
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');
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы