티스토리 뷰

Dev/Spring

Spring Boot V - Controller Hierachcy

마이스토리 2016. 6. 7. 15:14

> Creating Meaningful RESTful Web Service Controller Class Hierachies with Spring Boot


* Base Controller 클래스를 활용하여 exception Handler 추가

> 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());

}

}


> Base Controller
public class BaseController {

// The Logger for this class hierarchy.
protected Logger logger = LoggerFactory.getLogger(this.getClass());

@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> handleExcpetion(
Exception exception, HttpServletRequest request) {
logger.error("> handleExcpetion");
logger.error(" - exception: ", exception);

ExceptionAttributes exceptionAttributes = new DefaultExceptionAttributes();

Map<String, Object> responseBody = exceptionAttributes
.getExceptionAttributes(exception, request,
HttpStatus.INTERNAL_SERVER_ERROR);

logger.error("< handleExcpetion");
return new ResponseEntity<Map<String, Object>>(responseBody,
HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(NoResultException.class)
public ResponseEntity<Map<String, Object>> handleNoResultException(
NoResultException noResultException, HttpServletRequest request) {
logger.error("> handleNoResultException");
logger.error(" - excepiton: " + noResultException);

ExceptionAttributes exceptionAttributes = new DefaultExceptionAttributes();

Map<String, Object> responseBody = exceptionAttributes
.getExceptionAttributes(noResultException, request,
HttpStatus.NOT_FOUND);

logger.error("< handleNoResultException");
return new ResponseEntity<Map<String, Object>>(responseBody,
HttpStatus.NOT_FOUND);
}

}


> 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
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31