Spring boot'ta exception handling yapmak istiyorum. Ama yazdığım handler devreye girmiyor. Sebebini araştırdığımda pek bir sonuç elde edemedim. Sadece @EnableWebMvc'yi eklemek gerektiği ve Priority'sinin tanımlanması gerektiği yazılmış. Oda çözüm olmadı.

Handler:

@ControllerAdvice(annotations = RestController.class)
@Priority(1)
@ResponseBody
public class ExceptionHandlerTranslator extends ResponseEntityExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlerTranslator.class);

    @ExceptionHandler({RuntimeException.class})
    @ResponseStatus(HttpStatus.CONFLICT)
    ResponseEntity handleException(RuntimeException exception,
                                   HttpHeaders headers,
                                   HttpStatus status,
                                   WebRequest request){
        logger.debug("DataIntegrityViolationException",exception.getLocalizedMessage());
        logger.trace(exception.getMessage(),exception);
        return getResponseEntity("exception.type.database", "exception.dataIntegrityViolationException", true, headers, status);
    }

    private ResponseEntity getResponseEntity(String type, String message, boolean translate, HttpHeaders headers, HttpStatus status) {
        return new ResponseEntity(new ErrorMessage(type, message, translate), headers, status);
    }
}

Controller:

@RestController
@RequestMapping("/product/group")
public class ProductGroupRestController {

    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ProductGroupDto save(@RequestBody final ProductGroupDto productGroupDto){
        throw new RuntimeException(new String("Deneme"));
    }
}

ApplicationConfig:

@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@ComponentScan("com.arga.albaga")
public class ApplicationConfig extends WebMvcConfigurerAdapter {

Loglarda Handleri görüyor:

.02:02:47.044 [RMI TCP Connection(2)-127.0.0.1] INFO  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Detected @ExceptionHandler methods in exceptionHandlerTranslator 
.02:02:47.044 [RMI TCP Connection(2)-127.0.0.1] INFO  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Detected @ExceptionHandler methods in repositoryRestExceptionHandler

Ama Hatayı Yakalayamıyor:

.02:08:50.919 [http-nio-8080-exec-10] ERROR o.s.boot.web.support.ErrorPageFilter - Forwarding to error page from request [/product/group/save] due to exception [Deneme] 
java.lang.RuntimeException: Deneme
    at com.arga.albaga.module.product.presentation.ProductGroupRestController.save(ProductGroupRestController.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

soruldu: 07 Eyl, 19:19

SaRPaRDa's gravatar image

SaRPaRDa
5.0k213165
cevap kabul oranı: 27%

Bu soruya ilk cevap veren sen ol!
toggle preview

Bu soruyu takip et

E-Posta üzerinden:

Üyelik girişi yaptıktan sonra abonelik işlemlerini yapabilirsiniz

RSS üzerinden:

Cevaplar

Cevaplar ve Yorumlar

Yazı Formatlama

  • *italic* ya da _italic_
  • **bold** ya da __bold__
  • link:[text](http://url.com/ "başlık")
  • resim?![alt text](/path/img.jpg "başlık")
  • liste: 1. Foo 2. Bar
  • temel HTML etiketleri de kullanılabilir

Bu sorunun etiketleri:

×4
×4
×1

Soruldu: 07 Eyl, 19:19

Görüntüleme: 90 kez

Son güncelleme: 07 Eyl, 19:19

powered by BitNami OSQA