클라이언트가 서버에게 요청을 보낼때 크게 4가지로 분류할수 있습니다!
{
Key Value
"name" : "이재선",
"job" : "학생"
}
기능 | Method | URL |
---|---|---|
친구 생성 | POST | /api/person |
친구 목록 조회 | GET | /api/person |
친구 정보 변경 | PUT | /api/person/{id} |
친구 정보 삭제 | DELETE | /api/person/{id} |
package com.sparta.week01.entity;
import com.sparta.week01.dto.PersonRequestDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Getter
@NoArgsConstructor
@Entity
public class Person {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int age;
public Person(PersonRequestDto requestDto) {
this.name = requestDto.getName();
this.age = requestDto.getAge();
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void update(PersonRequestDto requestDto) {
this.name = requestDto.getName();
this.age = requestDto.getAge();
}
}
package com.sparta.week01.repository;
import com.sparta.week01.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person, Long> {
}
package com.sparta.week01.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Getter
public class PersonRequestDto {
private String name;
private int age;
public PersonRequestDto(String name, int age){
this.name = name;
this.age = age;
}
}
package com.sparta.week01.service;
import com.sparta.week01.dto.PersonRequestDto;
import com.sparta.week01.entity.Person;
import com.sparta.week01.repository.PersonRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@RequiredArgsConstructor
@Service
public class PersonService {
public final PersonRepository personRepository;
@Transactional // sql작업선언
public Long update(Long id, PersonRequestDto requestDto) {
Person person1 = personRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("해당 아이디가 존재하지 않습니다!")
);
person1.update(requestDto);
return person1.getId();
}
}