• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

2-3 진행 중 에러 발생

24.01.31 20:19 작성 24.01.31 22:05 수정 조회수 140

0

2-3 강의 중 12분 쯤에 Send를 누르면 200 OK가 떠야한다고 하셨는데 저는 500 에러가 발생합니다!

package com.jyujyu.review.api;

import com.jyujyu.review.service.TestService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
public class TestEntityApi {
    private final TestService testService;

    @PostMapping("/test/entity/create")
    public void createTestEntity(
            @RequestBody CreateTestEntityRequest request
    ) {
        testService.create(request.getName(), request.getAge());
    }

    @AllArgsConstructor
    @Getter
    public static class CreateTestEntityRequest {
        private final String name;
        private final Integer age;
    }
}
package com.jyujyu.review.api;

import com.jyujyu.review.service.TestService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
public class TestEntityApi {
    private final TestService testService;

    @PostMapping("/test/entity/create")
    public void createTestEntity(
            @RequestBody CreateTestEntityRequest request
    ) {
        testService.create(request.getName(), request.getAge());
    }

    @AllArgsConstructor
    @Getter
    public static class CreateTestEntityRequest {
        private final String name;
        private final Integer age;
    }
}
package com.jyujyu.review.repository;

import com.jyujyu.review.model.TestEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TestRepository extends JpaRepository<TestEntity, Long> {
    // Long -> TestEntity에 @Id가 있는 필드의 자료형을 작성한다.

}
package com.jyujyu.review.model;

import jakarta.persistence.*;
import lombok.Getter;

@Getter
@Table(name = "test")
@Entity
public class TestEntity {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private Integer age;

    public TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public TestEntity() {
    }

    public void changeNameAndAge(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.2'
	id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.jyujyu'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

repositories {
	mavenCentral()
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	runtimeOnly 'com.h2database:h2'
}

tasks.named('test') {
	useJUnitPlatform()
}
server.port=8081

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:rss
spring.datasource.username=sa
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true


알려주신대로 코드를 작성하고 포스트맨에서 Send를 눌러도 500 Internal Server:Error 라고 뜨네요 ㅠ 원인을 찾아봐도 안보여서 여쭙습니다..! 참고로 포트는 8081이 맞습니다

혹시 파일 필요하실까봐 링크 첨부했습니다
https://drive.google.com/file/d/1W65YQNY5rOGjWuqr-ntff7_QuTWz4eDw/view?usp=sharing

답변 2

·

답변을 작성해보세요.

0

안녕하세요 ! 지식공유자 쥬쥬입니다.

 

혹시 Exception Log를 첨부해주실 수 있으실까요?

 

감사합니다.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.01.31

안녕하세요, 인프런 AI 인턴입니다. liltjay님, HTTP 500 Internal Server Error 문제는 꽤 복잡할 수 있으며 다양한 원인이 있을 수 있습니다.

질문 내용을 바탕으로 유사한 경우를 찾아보았고 다음 링크에서 비슷한 문제에 대한 토론을 볼 수 있습니다:

  • 2-3 진행중 에러 - 이 질문에서는 2-3 강의 중 DELETE 작업에서 유사한 500 에러가 발생했고, 지식공유자 쥬쥬는 TestEntity가 존재하지 않을 때 발생할 수 있다고 언급했습니다.

위의 경우와 비슷하게, HTTP 500 에러는 서버 내부에서 발생한 예외 때문에 발생할 수 있습니다. 일반적으로는 서버 코드의 버그, 데이터베이스 연결 문제, 필요한 데이터의 부재, 또는 서버의 리소스 부족 등 다양한 원인이 있을 수 있습니다.

문제를 진단하기 위해 서버의 로그를 확인해 보시거나, 일반적으로는 예외를 처리하는 코드를 넣어 예외 정보를 포착하여 진단에 도움을 줄 수 있습니다. 또한 @ExceptionHandler@ControllerAdvice를 사용하여 글로벌 예외 핸들러를 구현하여 보다 명확한 오류 메시지를 얻을 수 있습니다.

현재 제공된 코드에서는 TestEntity의 생성자나 다른 구성 요소에서 예외가 발생할 가능성이 있기 때문에, 정확한 원인을 찾기 위해서는 이러한 방법을 시도해 볼 필요가 있습니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.