Jackson 오류

Junyoung·2024년 4월 4일

trouble

목록 보기
5/8

2024-03-28T17:57:48.001+09:00 ERROR 38812 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.ssafy.newstar.domain.article.dto.ArticleDetailResponse]] with root cause

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.ssafy.newstar.domain.article.dto.ArticleDetailResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.ssafy.newstar.util.response.SuccessResponseEntity["data"])

@Data를 붙이지 않으니 Json 으로 변환해서 데이터 랜더링을 못했던 오류이다 !
JSON 직렬화 변환 과정에서는 Jackson 라이브러를 사용하기 때문에
Json -> 객체 = Setter 사용,
객체 -> Json = Getter 사용

따라서 해당 메소드가 존재하지 않는다면 에러가 나는 것이다 !

참조 : https://mommoo.tistory.com/83


package com.ssafy.newstar.domain.record.dto;

import com.ssafy.newstar.domain.article.entity.Article;
import java.util.List;
import lombok.Data;
import lombok.ToString;


public class RecordResponse {

  Long id;

  private String title;

  private Integer scategory;

  public static RecordResponse createRecordResponse(Article article) {
    RecordResponse recordResponse = new RecordResponse();
    recordResponse.id = article.getId();
    recordResponse.title = article.getTitle();
    recordResponse.scategory = article.getScategory();
    return recordResponse;
  }

  public static List<RecordResponse> createRecordResponse(List<Article> articles) {
    return articles.stream().map(RecordResponse::createRecordResponse).toList();
  }
}

@Data
public class RecordResponse

해당 설정으로 에러를 해결할 수 있었다.

profile
라곰

0개의 댓글