자바 GET, POST, PUT, DELETE API 호출

김예진·2022년 11월 7일
0

java

목록 보기
6/21

웹개발의 봄, spring 강의를 듣고 api 호출 연습을 해보았다

먼저 dto, entity, repository를 만들어준다

dto

package com.homework.week01.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@RequiredArgsConstructor
public class PersonRequestDto {

    private String name;
    private Long age;
    private String address;
}

@RequiredArgsConstructor: 생성자들을 자동으로 생성

entity

package com.homework.week01.entity;

import com.homework.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
    private String name;

    @Column
    private Long age;

    @Column
    private String address;

    public Person(PersonRequestDto requestDto){
        this.name = requestDto.getName();
        this.age = requestDto.getAge();
        this.address = requestDto.getAddress();
    }

    public void Update(PersonRequestDto requestDto){
        this.name = requestDto.getName();
        this.age = requestDto.getAge();
        this.address = requestDto.getAddress();
    }

}

@Generated Value (Strategy = GenerationType.AUTO): 자동증가
@NoArgsConstructor: 기본생성자 생성
repository

package com.homework.week01.repository;

import com.homework.week01.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}

@Controller

package com.homework.week01.controlloer;

import com.homework.week01.dto.PersonRequestDto;
import com.homework.week01.entity.Person;
import com.homework.week01.repository.PersonRepository;
import com.homework.week01.service.PersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
public class PersonController {

    private final PersonRepository personRepository;
    private final PersonService personService;

    @PostMapping("/api/persons")
    public Person createPerson(@RequestBody PersonRequestDto requestDto){
        Person person = new Person(requestDto);

        return personRepository.save(person);
    }

    @GetMapping("/api/persons")
    public List<Person> showPerson(){
        return personRepository.findAll();
    }

    @PutMapping("/api/persons/{id}")
    public Long editPerson(@PathVariable Long id, @RequestBody PersonRequestDto requestDto){
        return personService.update(id, requestDto);
    }

    @DeleteMapping("/api/persons/{id}")
    public Long delPerson(@PathVariable Long id){
        personRepository.deleteById(id);

        return id;
    }
}

@RequiredArgsConstructor: 생성자들을 자동으로 생성
@PathVariable: 변수 이어주는 어노테이션

service

package com.homework.week01.service;

import com.homework.week01.dto.PersonRequestDto;
import com.homework.week01.entity.Person;
import com.homework.week01.repository.PersonRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@RequiredArgsConstructor
@Service
public class PersonService {

    private final PersonRepository personRepository;

    @Transactional
    public Long update(Long id, PersonRequestDto requestDto){
        Person person = personRepository.findById(id).orElseThrow(
                () -> new IllegalArgumentException("해당 아이디가 존재하지 않습니다.")
        );
        person.Update(requestDto);

        return person.getId();
    }
}

@RequiredArgsConstructor: 초기화되지 않은 final 필드 생성

0개의 댓글