오늘 공부한 내용은 저번시간에 이어서 put방식과 delete방식, Response내려주기에 대해 실습해 보았다.
우선 put방식에 대해 작성해보겠다. 우선 저번시간에 계속 보던 사진을 가져오겠다.
PutApiController
package com.example.put;
import com.example.put.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class PutApiController {
@PutMapping("/put")
public PostRequestDto put(@RequestBody PostRequestDto requestDto) {
System.out.println(requestDto);
return requestDto;
}
}
PostRequestDto
package com.example.put.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.List;
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PostRequestDto {
private String name;
private int age;
private List<CarDto> carList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<CarDto> getCarList() {
return carList;
}
public void setCarList(List<CarDto> carList) {
this.carList = carList;
}
@Override
public String toString() {
return "PostRequestDto{" +
"name='" + name + '\'' +
", age=" + age +
", carList=" + carList +
'}';
}
}
CarDto
package com.example.put.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
public class CarDto {
private String name;
@JsonProperty("car_number")
private String carNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
@Override
public String toString() {
return "CarDto{" +
"name='" + name + '\'' +
", carNumber='" + carNumber + '\'' +
'}';
}
}
put방식 테스트 및 결과화면
다음으로는 delete방식에 대해서 작성하겠다.
DeletApiController
package com.example.delete.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class DeletApiController {
@DeleteMapping("/delete/{userId}")
public void delete(@PathVariable String userId, @RequestParam String account) {
System.out.println(userId);
System.out.println(account);
}
}
delete방식 테스트 및 결과화면
다음 으로는 Response내려주기에 대해 작성하겠다.
ApiController
package com.example.reponse.controller;
import com.example.reponse.dto.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class ApiController {
// TEXT
@GetMapping("/text")
public String text(@RequestParam String account) {
return account;
}
//JSON
@PostMapping("/json")
public User json(@RequestBody User user) {
return user;
}
@PutMapping("/put")
public ResponseEntity<User> put(@RequestBody User user) {
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
}
PageController
package com.example.reponse.controller;
import com.example.reponse.dto.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class ApiController {
// TEXT
@GetMapping("/text")
public String text(@RequestParam String account) {
return account;
}
//JSON
@PostMapping("/json")
public User json(@RequestBody User user) {
return user;
}
@PutMapping("/put")
public ResponseEntity<User> put(@RequestBody User user) {
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
}
User
package com.example.reponse.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class User {
private String name;
private int age;
private String phoneNumber;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", phoneNumber='" + phoneNumber + '\'' +
", address='" + address + '\'' +
'}';
}
}
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Main Html Spring Boot
</body>
</html>
이상으로 오늘 공부한 내용에 대해 작성했다. 내일은 쉬는날이니 쉬고 오겠다 ㅎㅎ
그럼 배위 ~~
'Spring Boot' 카테고리의 다른 글
AI 검증 비즈니스 프로젝트 (0) | 2024.08.23 |
---|---|
IoC, DI (0) | 2023.04.22 |
Object Mapper에 모범사례 실습 (0) | 2023.04.22 |
GET(QueryParame ter), POST (0) | 2023.04.22 |
GET API 실습 (0) | 2023.04.22 |