티스토리 뷰
> Creating Meaningful RESTful Web Service Controller Class Hierachies with Spring Boot
> ExceptionAttributes 인터페이스 및 class 생성
public interface ExceptionAttrubutes {
Map<String, Object> getExceptionAttributes(Exception exception, HttpServletRequest httpRequest, HttpStatus httpStatus);
}
public class DefaultExceptionAttributes implements ExceptionAttrubutes {
public static final String TIMESTAMP = "timestamp";
public static final String STATUS = "status";
public static final String ERROR = "error";
public static final String EXCEPTION = "exception";
public static final String MESSAGE = "message";
public static final String PATH = "path";
@Override
public Map<String, Object> getExceptionAttributes(Exception exception, HttpServletRequest httpRequest, HttpStatus httpStatus) {
Map<String, Object> exceptionAttributes = new LinkedHashMap<String, Object>();
exceptionAttributes.put(TIMESTAMP, new Date());
addHttpStatus(exceptionAttributes, httpStatus);
addExceptionDetail(exceptionAttributes, exception);
addPath(exceptionAttributes, httpRequest);
return exceptionAttributes;
}
private void addHttpStatus(Map<String, Object> exceptionAttributes, HttpStatus httpStatus){
exceptionAttributes.put(STATUS, httpStatus.value());
exceptionAttributes.put(ERROR, httpStatus.getReasonPhrase());
}
private void addExceptionDetail(Map<String, Object> exceptionAttributes, Exception exception){
exceptionAttributes.put(EXCEPTION, exception.getClass().getName());
exceptionAttributes.put(MESSAGE, exception.getMessage());
}
private void addPath(Map<String, Object> exceptionAttributes, HttpServletRequest httpRequest){
exceptionAttributes.put(PATH, httpRequest.getServletPath());
}
}
> Service 단에서 적절한 exception throw
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
@CachePut(value="greetings", key="#result.id")
public Greeting create(Greeting greeting){
counterService.increment("method.invoked.greetingServiceBean.create");
if (greeting.getId() != null){
// Cannot create Greeting with specified ID value
logger.error(
"Attempted to create a Greeting, but id attribute was not null.");
throw new EntityExistsException(
"The id attribute must be null to persist a new entity.");
}
Greeting savedGreeting = greetingRepository.save(greeting);
// Illustrate Tx rollback
if (savedGreeting.getId() == BigInteger.valueOf(4L)){
throw new RuntimeException("Roll me back!!");
}
return savedGreeting;
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
@CachePut(value="greetings", key="#greeting.id")
public Greeting update(Greeting greeting){
counterService.increment("method.invoked.greetingServiceBean.update");
Greeting greetingPersisted = findOne(greeting.getId());
if (greetingPersisted == null){
// Cannot update Greeting that hasn't been persisted.
logger.error(
"Attempted to update a Greeting, but the entity does not exist.");
throw new NoResultException("Requested entity not found.");
}
Greeting updatedGreeting = greetingRepository.save(greeting);
return updatedGreeting;
}
> not found excetion에 대한 response 예
{
"timestamp": 1465279762271,
"status": 404,
"error": "Not Found",
"exception": "javax.persistence.NoResultException",
"message": "Requested entity not found.",
"path": "/api/greetings/6"
}
> 기타 exceptoin 에 대한 response 예
{
"timestamp": 1465279984510,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.persistence.EntityExistsException",
"message": "The id attribute must be null to persist a new entity.",
"path": "/api/greetings"
}
- Total
- Today
- Yesterday