예 > 이전 SOAP 방식
http://localhost:8090/app/delMember?userid=1
REST 방식에서 회원을 삭제하는 작업
http://localhost:8090/app/delMember/1
이전 SOAP 방식
REST 방식
기본적으로 form을 사용하는 html에서는 GET/POST 방식만 지원된다
따라서 도구가 필요하다 (curl, postman, 크롬브라우저의 플러그인 Talend Test API)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
` # application.properties
logging.level.com.exam=info
logging.level.org.springframework=info
# tomcat port 번호 변경
server.port=8090
# context 명 변경
server.servlet.context-path=/app`
가. pom.xml 등록
나. 현재 사용중인 sts(eclipse)에 lombok 설정
https://projectlombok.org/download 가서 lombok 다운
C:\ 에 저장
cmd 창 열고 cd C:\
lombok.jar 실행
java -jar lombok.jar



maven update 하기
sts 재시작
1) 의존성
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
2) application.properties 설정
# DB 연동위한 4가지 정보 설정
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=1234
# 별칭
mybatis.type-aliases-package=com.exam.dto
# mapper 등록
mybatis.mapper-locations=com/exam/mapper/*Mapper.xml`
3) table 생성
`
use testdb;
create table person (
id int primary key,
username varchar(20) not null,
birthdate date
);
insert into person (id, username, birthdate ) values ( 10, '홍길동', current_date() );
insert into person (id, username, birthdate ) values ( 20, '이순신', current_date() );
insert into person (id, username, birthdate ) values ( 30, '유관순', current_date() );
commit;
4) PersonDTO.java ( lombok 사용 )
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Getter
@Alias("PersonDTO")
public class PersonDTO {
int id;
String username;
LocalDate birthdate;
}
5) PersonMapper.xml 와 PersonMapper.java
<mapper namespace="com.exam.mapper.PersonMapper">
<select id="findAll" resultType="PersonDTO">
select id, username, birthdate
from person
order by id desc
</select>
</mapper>
@Mapper
public interface PersonMapper {
public List<PersonDTO> findAll();
}
6) PersonService.java , PersonServiceImpl.java
public interface PersonService {
public List<PersonDTO> findAll();
}
@Service
public class PersonServiceImpl implements PersonService {
PersonMapper personMapper;
public PersonServiceImpl(PersonMapper personMapper) {
this.personMapper = personMapper;
}
@Override
public List<PersonDTO> findAll() {
return personMapper.findAll();
}
}
7) PersonController.java

예 SOAP 방식
GET http://localhost:8090/app/persons?id=10
GET http://localhost:8090/app/persons?id=20
=> @GetMapping("/"/persons)
@RequestParam("id") String id
REST방식
GET http://localhost:8090/app/persons/10
GET http://localhost:8090/app/persons/20
@GetMapping("/persons/{변수}") <= URL은 마지막 값까지 포함
@PathVariable("변수") String id
GET http://localhost:8090/app/persons/10/서울
GET http://localhost:8090/app/persons/20/부산
@GetMapping("/persons/{id}/{주소}")
@PathVariable("변수1") String id, @PathVariable("변수2") String address
GET http://localhost:8090/app/persons/10/address/서울
GET http://localhost:8090/app/persons/20/address/부산
@GetMapping("/persons/{id}/address{주소}")
@PathVariable("변수1") String id, @PathVariable("변수2") String address
Security 주요 고려사항( 인증 및 인가 Authentication and Authorization) ,csrf 보호, 데이터 검증 및 유효성 검사(Validation),
cors 설정(다른 도메인에서 API를 호출할 때), HTTP 메서드 보안(특정 HTTP 메서드에 대한 접근 제한), 입력 데이터 암호화,
로깅 및 모니터링
POST 방식.
전달되는 데이터는 JSON 형식.
{
"id":40,
"username":"유관순", ==============> PersonDTO 에 저장. (@RequestBody) 로 저장
"birthdate":"2024-12-23"
}
header에 전달되는 타입을 지정해애 된다.
Content-Type: application/json
LocalDate 사용하는 경우에는 날짜 포맷 설정 필요
LocalDate 날짜 포맷 지정
spring.mvc.format.date=yyyy-MM-dd

put 방식
JSON + @PathVariable
{
"username":"유관순2",
"birthdate":"2024-12-25" => (@RequestBody) 와 @PathVariable 같이 사용 가능
}
header에 전달되는 타입을 지정해애 된다.
Content-Type: application/json





2xx (Successful)
클라이언트의 요청을 성공적으로 처리
200 OK → 요청 성공
201 Created → 요청 성공해서 새로운 리소스가 생성
202 Accepted → 요청이 접수되었으나 처리가 완료되지 않음
204 No Content → 서버가 요청을 성공적으로 수행했지만, 응답 페이로드 본문에 보낼 데이터가 없음
3xx - 리다이렉션
3xx (Redirection)
요청을 완료하기 위해 유저 에이전트의 추가 조치 필요
특징: HTTP를 단순한 전송 메커니즘으로만 사용합니다.
설명: 리소스 식별이나 HTTP 메서드의 사용이 없이, XML 또는 JSON을 전송하는 데에만 초점을 맞춥니다. 이는 단순한 원격 프로시저 호출(RPC) 방식과 유사합니다.
예시: 모든 요청이 동일한 URL로 전송되고, 동작의 구분이 URL이 아닌 요청 본문의 내용에 의해 결정됩니다.
특징: 개별 리소스에 대한 고유한 식별자(URI)를 사용합니다.
설명: 각 리소스를 고유한 URI로 식별하지만, HTTP 메서드(GET, POST, PUT, DELETE 등)는 제대로 활용되지 않습니다.
예시 :
GET /articles
GET /articles/123
특징: HTTP 메서드를 활용하여 리소스에 대한 다양한 작업을 수행합니다.
설명: 리소스를 고유한 URI로 식별하며, HTTP 메서드를 통해 리소스에 대한 동작을 정의합니다. GET, POST, PUT, DELETE 등의 메서드를 적절히 사용합니다.
예시:
GET /articles - 모든 기사 조회
GET /articles/123 - 특정 기사 조회
POST /articles - 새로운 기사 작성
PUT /articles/123 - 특정 기사 업데이트
DELETE /articles/123 - 특정 기사 삭제
특징: 응답에 하이퍼미디어 링크를 포함하여, 클라이언트가 리소스 상태 전이를 동적으로 발견하고 사용할 수 있도록 합니다.
설명: API 응답에 포함된 링크를 통해 클라이언트가 다음 가능한 작업을 발견할 수 있도록 하여, 클라이언트와 서버 간의 상호작용을 더욱 유연하게 만듭니다.
예시:
{
"id": 123,
"title": "RESTful API Design",
"content": "Some content",
"_links": {
"self": {
"href": "/articles/123"
},
"update": {
"href": "/articles/123",
"method": "PUT"
},
"delete": {
"href": "/articles/123",
"method": "DELETE"
},
"author": {
"href": "/authors/1"
}
}
}
0단계: HTTP는 단순한 전송 메커니즘으로 사용됩니다.
1단계: 리소스를 URI로 식별하지만, HTTP 메서드는 제대로 활용되지 않습니다.
2단계: 리소스를 URI로 식별하고, HTTP 메서드를 통해 리소스에 대한 다양한 작업을 수행합니다.
3단계: API 응답에 하이퍼미디어 링크를 포함하여, 클라이언트가 동적으로 상태 전이를 발견하고 사용할 수 있게 합니다.
현재는 요청한 데이터가 없어도 status 값은 200 이고 body는 No Content로 반환됨.
이상황을 좀 더 직관 적인 status 값을 404로 알려주자
가. 사용자 예외 클래스 생성
나.

return ResponseEntity.status(HttpStatus.CREATED).build(); // 201
REST에서는 바인딩result 필요없고 @Valid만 필요

src/main/resource
bundle
message.properties (기본)
message_en.properties (영어)
message_ko.properties (한국어)


public class PersonDTO {
int id;
@JsonProperty("user_name")
String username;
@JsonIgnore
LocalDate birthdate;
}
가. header
type : jwt
암호화 알고리즘
나. payload
다. signature
request :
{
"userid":"user",
"passwd":"1234",
추가정보:값
}
response :
{
"token":토큰값
}
=> react 에서 local storage에 저장됨
===> react에서 local storage에 저장해두고 필요시 사용됨.
{
Authorization : BEARER 토큰값
}