728x90
반응형
- 성공 시 { success : true, result : data }
- 실패 시 { success : false, result : "에러 사유" }
1. CommonResponse
backend/response/CommonResponse
package web.backend.response;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class CommonResponse<T> {
private boolean success;
private T result;
}
이제 컨트롤러에서 성공 시 반환할 데이터를 수정해보자.
2. UserController
backend/module/user/UserController
package web.backend.module.user;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import web.backend.annotation.LogTrace;
import web.backend.module.user.repository.UserUpdateDto;
import web.backend.response.CommonResponse;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
private final UserService userService;
@GetMapping
public CommonResponse<List<User>> findAll() {
return new CommonResponse<List<User>>(true, userService.findAll());
}
@GetMapping("/{userId}")
public CommonResponse<User> findByIndexId(@PathVariable(value = "userId") Long userId ) {
return new CommonResponse<User>(true, userService.findByIndexId(userId));
}
@PostMapping
public CommonResponse<String> save(@RequestBody User user) {
return new CommonResponse<String>(true, userService.save(user));
}
@PatchMapping
public CommonResponse<String> update(@RequestBody UserUpdateDto user) {
return new CommonResponse<String>(true, userService.update(user.getId(), user.getUser()));
}
@DeleteMapping
public CommonResponse<String> delete(@RequestParam Long id) {
return new CommonResponse<String>(true, userService.delete(id));
}
}
테스트
POST Method
GET Method
728x90
반응형