starter.spring.io 에서 Dependencies에 Spring Web을 추가한다.
Generate 후 생성된 파일을 압축 해제하고 생성된 폴더를 IDE에 추가한다
demo.src.main.java.com.example.demo 폴더에 Greeting.java 파일을 생성한 후 record 클래스를 생성하고 매개변수로 long id, String content를 설정한다
record 클래스에 대한 설명
Record (Java SE 19 & JDK 19)
public abstract class Record extends Object This is the common base class of all Java language record classes. More information about records, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.
docs.oracle.com
Resource Controller를 생성하기 위해 GreetingController.java를 demo\src\main\java\com\example\demo에 생성하고
http://localhost:8080/greeting 으로 오는 Get 요청 Http requests를 다루기 위해 @RestController와 @GetMapping을 설정하고 Greeting 객체에 counter.incrementAndGet(), String.format(template, name)을 인수로 넘긴 후 반환한다.
AtomicLong 클래스는 자동적으로 업데이트 될 수 있는 long 타입의 값이다.
생성자에 인수로 아무값을 넣지않고 객체를 생성하면 0부터 시작하는 AtomicLong 객체를 생성한다.
AtomicLong.incrementAndGet() 함수는 현재 값을 자동적으로 증가시킨다.
AtomicLong (Java SE 19 & JDK 19)
All Implemented Interfaces: Serializable A long value that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses. An AtomicLong is used in applications such as atomically incremented sequence numbe
docs.oracle.com
DemoApplication.java를 실행시키고 http://localhost:8080/greeting, http://localhost:8080/greeting?name=holy_season으로 접속하면 다음과 같은 화면이 나온다.
'Spring' 카테고리의 다른 글
jjwt 서버 적용 오류 (0) | 2023.06.12 |
---|---|
스프링 DATA JPA와 Mysql 사용 시 원하는 표기법으로 컬럼명 생성하기 (0) | 2023.05.13 |
Requesting Java AST from selection 오류 해결법 (0) | 2023.03.11 |
반복 작업 만들기 (0) | 2023.02.02 |
스프링 부트를 이용해 웹서버 구축하기 (0) | 2023.01.30 |