Optional
Null 값 방지 용도
자바 클래스
https://tinylittlelife.tistory.com/153
< 예제 >
선택 조회시
( 1건 조회 )
Service
Optional.ofNullable()
/**
* id(기본키)로 조회 : 상세조회(1건 조회)
* 기능 업그레이드 null 방지
* @param dno
* @return
*/
public Optional<Dept> findById(long 기본키) {
객체 객체변수 = deptDao.selectById(기본키);
Optional<객체> optionalDept = Optional.ofNullable(객체변수);
return optionalDept;
}
Controller
get()
@GetMapping("/경로/{기본키}")
public ResponseEntity<Object> getById(@PathVariable long 기본키) {
try{
Optional<객체> optional = Service.findById(dno);
if(optional.isEmpty() == false) {
return new ResponseEntity<>(optional.get(), HttpStatus.OK);
} else {
// TODO: 조회는 성공했으나 데이터가 없음
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
} catch (Exception e) {
log.debug(e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
'SpringBoot > code' 카테고리의 다른 글
SpringBoot - SSR에서 페이징 처리에 필요한 객체 PageReq , PageRes (0) | 2023.10.12 |
---|---|
SpringBoot - model / common package (0) | 2023.10.12 |
SpringBoot - RedirectView (클래스) (0) | 2023.10.10 |
SpringBoot - @ResponseBody (1) | 2023.10.10 |
SpringBoot - model - 등록일 수정일 추가 (0) | 2023.10.10 |