InvalidDefinitionException

한지연·2023년 5월 18일
0

문제 상황: get요청으로 유저리스트를 가져올 때 500 error가 뜸
No serializer found for class com.group.libraryapp.dto.user.response.UserResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ImmutableCollections$ListN[0])

    @GetMapping("/user")
    public List<UserResponse> userList(){
        return users.stream().map(user ->
            new UserResponse(users.indexOf(user), user)).toList();
    }

문제 원인: 해당 api를 가져올 때 user를 UserResponseDto로 변환해서 가져오는데 이때 UserResponse를 객체로 바꿀 수 없음.


문제 해결 방법: UserResponse 객체에 getter 또는 기본 생성자를 추가해준다. (사람마다 문제가 되는 상황이 다르기 때문에 둘 다 필요할 수도 있고 둘 중에 하나만 추가해야 할 수도 있다.)

public class UserResponse {

    private long id;
    private String name;
    private Integer age;

    public UserResponse(long id, User user) {
        this.id = id;
        this.name = user.getName();
        this.age = user.getAge();
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }


이제 api 요청에 문제없이 응답한다

profile
배우고 활용하는 것을 즐기는 개발자, 한지연입니다!

0개의 댓글