본문 바로가기
공부용/스프링 || 스프링부트

어제 못한 프런트 제작하기

by alpakaka 2025. 2. 20.

오늘의 할 일

- 프런트 돌아가도록.. + 연결하기

- HabitLog 관련 컨트롤러 제작하기

 

개발일지

어제 리액트로 만드려다가 계속 tailwind 에서 에러가 나서 더 이상 진행을 못했었다. 일단 오늘까지 진행해보고 안되면 순수 react 로 해야할 것 같다.

sh: tailwind: command not found

어제 부터 계속 발생하던 오류이다..

npm list를 확인해도 계속 안된다고한다.

https://tailwindcss.com/docs/installation/using-postcss

 

Installing with PostCSS - Installation

Integrate Tailwind CSS with frameworks like Next.js and Angular.

tailwindcss.com

 

 여기를 참고해서 해결하려고 했다.

 

일단 문제는 안터지니까.. 된줄 알고 claude가 만들어준 react 코드를 넣었는데

이런 일이 발생했다. tailwindcss 가 하나도 적용되지 않아 발생한 문제인 것 같다.

간단하게 체크해보자.

<body>
      <h1 className="text-3xl font-bold underline">Hello world!</h1>
    </body>

이 코드를 app에 넣고 돌려보니

underline도, 볼드처리도 아무것도 없다.

 

많은 시간을 여기에 할애하는 것 같아서 그냥 tailwind를 전부 빼버리고 진행한다.

 

만들어진 화면

그런데 fetch로 불러오질 못하길래 찾아보니 cors 에러였다. 간단하게 아래의 코드를 controller 에 붙여서 해결했다. 

@CrossOrigin(origins = "http://localhost:3000")

 

으음... 옆에 나오는 친구를 오늘 할일로 하고 클릭을 하면 완료한 걸로 나오게 해보자.

근데 일단 그러려면 habit log controller 가 만들어져야한다. 그 후 url을 바탕으로 클릭하면 완료한 걸 보내도록 코드를 작성한다.

controller와 service 를 만들자.

package com.example.backend.service;

import com.example.backend.entity.HabitLog;
import com.example.backend.repository.HabitLogRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class HabitLogService {
    private final HabitLogRepository habitLogRepository;

    public HabitLogService(HabitLogRepository habitLogRepository) {
        this.habitLogRepository = habitLogRepository;
    }

    public List<HabitLog> getAllHabitLogs() {
        return habitLogRepository.findAll();
    }

    public Optional<HabitLog> getHabitLogById(Long id) {
        return getHabitLogById(id);
    }

    public HabitLog saveHabitLog(HabitLog habitLog) {
        return habitLogRepository.save(habitLog);
    }

    public void deleteHabitLog(Long id) {
        habitLogRepository.deleteById(id);
    }
}
package com.example.backend.controller;

import com.example.backend.entity.HabitLog;
import com.example.backend.service.HabitLogService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/habitLogs")
@CrossOrigin(origins = "http://localhost:3000")
public class HabitLogController {
    private final HabitLogService habitLogService;

    public HabitLogController(HabitLogService habitLogService) {
        this.habitLogService = habitLogService;
    }

    @GetMapping
    public List<HabitLog> getAllHabitLog() {
        return habitLogService.getAllHabitLogs();
    }
    @GetMapping("/{id}")
    public Optional<HabitLog> getHabitLogById(@PathVariable Long id) {
        return habitLogService.getHabitLogById(id);
    }

    @PostMapping
    public HabitLog createHabitLog(@RequestBody HabitLog habitLog) {
        return habitLogService.saveHabitLog(habitLog);
    }

    @DeleteMapping("/{id}")
    public void deleteHabitLogById(@PathVariable Long id) {
        habitLogService.deleteHabitLog(id);
    }
}

 

간단하게 만들었다. 이제 데이터를 넣고 결과를 봐보자.

 

문제가 발생했다. 저기에 habit 의 1번이 null로 나온다. 

Spring Boot에서 @ManyToOne 관계를 설정했을 때, 기본적으로 Hibernate는 id만 유지하고 관련 객체 정보를 가져오지 않는다.

라고 하는데.. 그러면 딱히 상관이 없는 건가? 한번 찾아본다.

 

https://ict-nroo.tistory.com/132

 

[JPA] 즉시 로딩과 지연 로딩(FetchType.LAZY or EAGER)

즉시 로딩과 지연 로딩프록시 학습 처음에 했던 질문. Member를 조회할 때 Team도 함께 조회 해야 할까?비즈니스 로직에서 단순히 멤버 로직만 사용하는데 함께 조회하면, 아무리 연관관계가 걸려

ict-nroo.tistory.com

 

이 블로그를 보니 그렇게 까지 문제는 없는 것 같다. 사실 그룹때문에 저 habit 이 중요한거지 지금 당장은 필요 없으니.. 그냥 이대로 계속 진행해도 괜찮아보인다.

 

대략 이런 느낌이었다.

 

일단 habit {1,2,3} 을 모두 넣어주고, getAll 메소드로 확인한다.

 잘 나온다. ^^b