[ERROR] No Properties Discovered To Create BeanSerializer

반응형

아래와 같이 Jackson을 사용해서 objectMapper에서 에러가 발생하면

import com.course.rabbitmqproducer.entity.Employee;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

@Service
public class EmployeeJsonProducer {

    private ObjectMapper objectMapper = new ObjectMapper();

    public void sendMessage(Employee emp){
        try {
            String json = objectMapper.writeValueAsString(emp);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

mapping한 Entity에 @JsonAutoDetect를 추가하면된다.

변경 전

@Getter
@Setter
public class Employee {
    private String employeeId;
    private String name;
    private LocalDate birthDate;

    public Employee(String employeeId, String name, LocalDate birthDate){
        super();
        this.employeeId = employeeId;
        this.name = name;
        this.birthDate = birthDate;
    }
}

변경 후

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDate;

@Getter
@Setter
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Employee {
    private String employeeId;
    private String name;
    private LocalDate birthDate;

    public Employee(String employeeId, String name, LocalDate birthDate){
        super();
        this.employeeId = employeeId;
        this.name = name;
        this.birthDate = birthDate;
    }
}

전체 에러 Stack은 아래와 같다.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.course.rabbitmqproducer.entity.Employee and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3905)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)
    at com.course.rabbitmqproducer.producer.EmployeeJsonProducer.sendMessage(EmployeeJsonProducer.java:20)
    at com.course.rabbitmqproducer.RabbitmqProducerApplication.run(RabbitmqProducerApplication.java:34)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.course.rabbitmqproducer.RabbitmqProducerApplication.main(RabbitmqProducerApplication.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

추천서적

 

스프링 부트와 AWS로 혼자 구현하는 웹 서비스

COUPANG

www.coupang.com

파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음


반응형

댓글

Designed by JB FACTORY