[springboot] lombok 의존성 추가 후 @Data 를 써도 나타나지 않는 getter, setter ..

FreeZeeSun·2024년 5월 31일
0

에러일기

목록 보기
9/9
post-thumbnail

문제상황

분명 DTO 에 @Data 어노테이션 잘 붙여줬고,
강의 진행하는대로 잘 따라했는데
나에게는 허용되지 않는 setter 와 getter 메서드...


전체 구성

SaveController - SaveDTO - ContentService - ContentEntity - ContentRepository


전체코드

SaveController

package com.example.demo.controller;


import com.example.demo.dto.SaveDTO;
import com.example.demo.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class SaveController {

    private ContentService contentService;
    
    @Autowired
    public SaveController(ContentService contentService) {
        this.contentService = contentService;
    }
    
    @PostMapping("/save")
    public String saveLogic(SaveDTO saveDTO) {
        contentService.saveContent(saveDTO);
        return "redirect:/";

    }
}

SaveDTO

package com.example.demo.dto;
import lombok.Data;

@Data
public class SaveDTO {

    private String title;
    private String content;

}

ContentService

package com.example.demo.service;

import com.example.demo.dto.SaveDTO;
import com.example.demo.entity.ContentEntity;
import com.example.demo.repository.ContentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentService {

    private ContentRepository contentRepository;

    @Autowired
    public ContentService(ContentRepository contentRepository) {
        this.contentRepository = contentRepository;
    }

    public void saveContent(SaveDTO saveDTO) {
        String title = saveDTO.getTitle(); //문제의 에러부분
        String content = saveDTO.getContent(); //문제의 에러부분

        ContentEntity content1 = new ContentEntity();

        content1.setTitle(title); //문제의 에러부분
        content1.setContent(content); //문제의 에러부분

        contentRepository.save(content1);
        
        return;
    }

    public List<ContentEntity> selectContent() {
        return contentRepository.findAll();
    }
}

ContentEntity

package com.example.demo.entity;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

@Entity
@Data
public class ContentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String title;
    private String content;

}

ContentRepository

package com.example.demo.repository;
import com.example.demo.entity.ContentEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ContentRepository extends JpaRepository<ContentEntity, Integer> {


}

해결과정

1. IDE 설정 확인

File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors > Enable annotation processing 체크!

챗지피티가 이걸 체크하라고 한다. Enable annotation processing
근데, 이걸 체크하든 안하든 아래의 결과는 변함 없었다.
고로 이 문제가 아니었다는것.

2. System.out.println 으로 getter 확인해보기


분명 sevice 까지 잘 오는게 확인됐다.

3. System.out.println 으로 setter 확인해보기

어라? 그러면.. 다 문제없이 잘되는건데?
근데 왜 빨간 줄이 나와서 에러인마냥 구는걸까.
그리고, 왜 MySQL DB에는 저장되지 않는건가...

4. System.out.println 으로 Repository 확인해보기

다 잘 되어있다. 빨간 줄만 뜰 뿐인건가?
MySQL DB 부분을 해결해보자.

어라..? 어제는 분명 null 로 잘못 입력 됐는데...
오늘은 또 너무 잘 된다.......
이유를 모르겠는게... 더.......... 안좋은거일텐데.......

결국, 처음부터 문제없던걸 문제있다고 생각했던 에러였다..

profile
개발자 지망생. 지금은 삽질의 연속, 하지만 언젠가는 삽질이 아닐 것이기에!

0개의 댓글