본문 바로가기
Spring

Springboot 3.x.x 를 사용해보자

by khds 2024. 1. 24.

 

들어가기

 

최근 Springboot 프로젝트를 생성하기 위해 Spring.io(https://start.spring.io/)에서 다운을 받으려고 들어갔더니 아래와 같이 나왔다.

 

 

Spring Boot의 버전과 Java 버전을 봐보자.

Springboot는 3.x.x이고 Java는 최소가 17이다. 

나는 Springboot 2.x.x 때 시작하였고 Java 버전은 항상 11 버전을 사용하였기에 어색한 모습이었다.

확인해 보니 Springboot 3.x.x는 22년 1월부터 정식 릴리즈 됐었고, 2.x.x 버전은 23년 11월까지 지원하는 것이었다. 

Springboot 프로젝트를 이전 버전으로 사용하려면 위의 방법으로 jar 파일로 다운로드하지 않고 gradle 혹은 maven에서 직접 하면 되기는 하지만, 계속해서 새로 나오는 기술들은 결국 터득해야 하는 것이기에 Springboot 3.x.x을 사용해 보기로 하였다.

이 글에서는 Intellij를 통해 Springboot 3.x.x를 사용하여 간단한 Api를 실행하겠다.

Springboot 3.x.x에 들어서 변화된 것들은 https://revf.tistory.com/260를 참고하길 바란다.

 

Spring Boot 3.0 무엇이 달라질까?

Spring Boot 3 (Spring Framework 6) 무엇이 달라질까? 지난 10월 20일 Spring Boot 3.0.0 RC1 가 릴리즈 되었습니다. 정식 버전 릴리즈는 11월 말 예정입니다. "We are not expecting any more features to be added at this point, and

revf.tistory.com

 

 

본론

Springboot 3.x.x를 다운받아 보자.

 

우선 Spring.io(https://start.spring.io/)에 들어가보자.

테스트 진행을 위해 아래와 같이 설정하고 다운로드하였다.

 

그다음 압축을 해제하고 서버를 실행하였더니 아래와 같은 에러가 발생하였다.

A problem occurred configuring root project 'demo'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.2.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.2
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.2 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.2 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

 

아직 코드도 짜지 않았는데 처음부터 막혀서 놀랐지만... 그리 어렵지 않게 문제를 해결할 수 있었다.

자세히 보면 Java Version이 잘못 설정된 것을 수 있다.

지금까지 스프링 부트 2.x.x 버전을 사용하면서 자바 버전을 11로 설정하였는데, 압축 파일을 다운로드할 때 설정한 자바의 버전은 17이다. 아래의 사진은 build.gradle 내용 중 일부이며 java 버전이 17로 설정된 것을 알 수 있다.

 

결국 버전을 맞춰주면 문제는 해결된다. 

우선 아래의 사이트를 통해 JDK 17 버전을 다운로드하고 환경변수에 등록해야 한다.

적용하는 방법은 아래의 글을 참고하자.

https://trace90.tistory.com/8

 

[Spring] Java SE 17 설치 및 환경 설정

Java SE 17 설치하기 Java Archive Downloads - Java SE 17 (oracle.com) Java Archive Downloads - Java SE 17 WARNING: These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest securi

trace90.tistory.com

 

설정 후  java --version 명령어를 통해 17 버전이 설치된 것을 확인하자.

 

이제 설치한 JDK를 프로젝트에 적용해야 한다. 

File - Project Settings - Project 내 SDK에 설치한 JDK 17 버전을 적용한다.

 

그다음 File - Settings - Build, Exception, Deployment - Build Tools - Gradle 내 Gradle JVM을 설치한 JDK 17 버전을 적용한다.

 

이렇게 자바 버전을 알맞게 적용하면 에러가 사라질 것이다.

 

Springboot 3.x.x를 사용해 보자.

 

이제 jpa와 h2를 통해 간단한 controller 테스트를 진행해 보자. 데이터를 저장하고 조회하는 예시이다.

 

우선 H2 데이터베이스를 적용하기 위한 설정을 해야 한다. application.properties 파일의 내용을 아래와 같이 입력한다.

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

 

그다음 간단하게 Controller와 Service를 작성한다.

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @PostMapping("/test")
    public ResponseEntity<Void> save(@RequestParam(value = "title") String title, @RequestParam(value = "content") String content) {
        testService.save(new TestEntity(null, title, content));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/test/{id}")
    public ResponseEntity<TestEntity> findById(@PathVariable(value = "id") Long id) {
        return ResponseEntity.ok().body(testService.findById(id));
    }
}

 

@Service
public class TestService {

    @Autowired
    private TestRepository testRepository;

    public TestEntity save(TestEntity testEntity) {
        return testRepository.save(testEntity);
    }

    public TestEntity findById(Long id) {
        return testRepository.findById(id).orElse(null);
    }
}

 

이제 POST 요청을 하면 아래와 같이 200 status 코드가 반환될 것이다.

 

실제 DB를 보면 데이터가 저장된 것을 알 수 있다.

 

이번엔 GET 요청을 해보자. 아래와 같이 결과가 나온다.

 

결론

 

이렇게 간단하게 Springboot 3.x.x 프레임워크를 사용하여 간단한 save, find 예제를 구현해보았다.

Springboot 3.x.x로 변하면서 Java 17 버전 이상이 필수적이기에 Springboot 2.x.x와 Java 11 버전을 사용했던 것에서 바꿔야 하는 부분이 꽤나 있을 것이다. 

여기서 보여준 부분들은 필자가 직접 개발을 하다가 초기에 발견된 문제로, 차후 개발을 진행하면서 이전과 달라진 부분이 더 발견될 수도 있다.

앞으로 그런 부분들이 생길 때마다 글을 업데이트하여 혼란스러운 부분이 생기지 않도록 해야겠다. 

 

 

참고

 

https://jojoldu.tistory.com/698

 

Spring Boot 3.x 실행이 안될 경우 (feat. IntelliJ)

스프링 부트 3.0 이 출시되었다. (현재 최신은 3.0.1 이지만) 3.0 부터는 Java 17부터 지원된다. 그러다보니 기존에 Java8, 11등을 사용하시던 분들은 3.x 스프링 부트 프로젝트를 실행할때 에러들을 만나

jojoldu.tistory.com

https://revf.tistory.com/260

 

Spring Boot 3.0 무엇이 달라질까?

Spring Boot 3 (Spring Framework 6) 무엇이 달라질까? 지난 10월 20일 Spring Boot 3.0.0 RC1 가 릴리즈 되었습니다. 정식 버전 릴리즈는 11월 말 예정입니다. "We are not expecting any more features to be added at this point, and

revf.tistory.com

https://trace90.tistory.com/entry/SpringBoot-Java설정-IntelliJ

 

[Spring] Spring Boot + Java SE 17 설정 with IntelliJ

SpringBoot 프로젝트 만들기 Spring Initializr Spring Initializr에서 Spring Boot 프로젝트를 쉽게 만들 수 있습니다. 위 화면과 같이 설정 후 Spring Boot프로젝트를 생성합니다. Java 설치가 필요하신 경우 참고해

trace90.tistory.com

https://velog.io/@akfls221/Spring-GraalVM-Native-Image

 

Spring GraalVM Native Image 띄어보기

GraalVm Native image와 Docker로 SpringBoot 실행해보기

velog.io

https://abbo.tistory.com/365

 

Spring Boot 3 버전으로 업그레이드

안녕하세요! 국내 웹개발자들은 대부분 알고 있다는 Spring Boot 가 이전에는 2.7 버전까지 릴리즈가 되어 있다가 최근에 공식적으로 3 버전이 릴리즈 되었다고 합니다. 이번 릴리즈는 11월 24일에 진

abbo.tistory.com