2016-11-18 31 views
1

我正在處理spring引導應用程序的異常處理。我創建了自己的異常類巫婆我扔,一切工作正常我在控制檯中得到異常,但我無法達到我的@ExceptionHandler方法。未達到Spring引導@ExceptionHandler

我的類,它拋出異常:

@Override 
public AuctionBody insertNewAuction(AuctionBody auctionBody, int ownerId, String AUCTION_TYPE) { 
    try { 
     SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("auctions") 
       .usingGeneratedKeyColumns("id"); 
     Map<String, Object> parameters = new HashMap<String, Object>(); 
     parameters.put("title", auctionBody.getTitle()); 
     parameters.put("type", auctionBody.getType()); 
     parameters.put("start_time", Timestamp.valueOf(auctionBody.getStartDate())); 
     parameters.put("end_time", Timestamp.valueOf(auctionBody.getEndDate())); 
     parameters.put("quantity", auctionBody.getItemQuantity()); 
     parameters.put("starting_price", auctionBody.getStartingPrice()); 
     parameters.put("currency", auctionBody.getCurrency()); 
     parameters.put("status", auctionBody.getStatus()); 
     parameters.put("description", null); 
     parameters.put("allow_bid_for_quantity", auctionBody.isAllowToBidForQuantity()); 
     parameters.put("buy_out_price", auctionBody.getBuyOutPrice()); 
     parameters.put("owner_id", ownerId); 
     parameters.put("buy_out_price", auctionBody.getBuyOutPrice()); 
     parameters.put("quantity_left", auctionBody.getItemQuantity()); 
     parameters.put("uuid", auctionBody.getAuctionIdUrlOwner()); 
     parameters.put("allow_buy_out", auctionBody.isAllowBuyOut()); 
     parameters.put("link", auctionBody.getLink()); 
     auctionBody.setId((Integer) simpleJdbcInsert.executeAndReturnKey(parameters)); 


     insertNewAuctionPictures(auctionBody, auctionBody.getId()); 
     if (AUCTION_TYPE.equals("FullAuction")) { 
      insertPeopleToInvite(auctionBody); 
     } 

     return auctionBody; 
    }catch (DataAccessException e){ 
     throw new JdbcExceptions("Cant add auction"); 
    } 
} 

例外是因爲描述爲空(我這樣做只是檢查,如果我會得到我的創建異常處理程序或沒有)的拋出。

Exception類看起來是這樣的:

public class JdbcExceptions extends RuntimeException { 
public JdbcExceptions(String message) { 
    super(message); 
} 

}

和異常處理程序控制器類看起來是這樣的:

@ControllerAdvice 
public class ExceptionHandlingController { 

    @ExceptionHandler(JdbcExceptions.class) 
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 
    public String getJdbcException(JdbcExceptions ex){ 
     return "redirect:/html/errorPage"; 
    } 
} 

我知道,它應該工作,我相信我有一些不良的配置,使我的@ExceptionHandler無法訪問,但我還沒有找到答案。 另外ExceptionHandlingController類是在應用程序運行時創建的。

主要類:

package com.visma.seli; 

import com.visma.seli.config.properties.repository.DatabaseProperties; 
import com.visma.seli.config.properties.repository.RepositoryProperties; 
import org.springframework.boot.Banner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.properties.EnableConfigurationProperties; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.web.servlet.DispatcherServlet; 

@SpringBootApplication 
@EnableConfigurationProperties({RepositoryProperties.class, DatabaseProperties.class}) 
@EnableScheduling 
@ComponentScan() 
@EnableAutoConfiguration 
public class SeliApplication extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(SeliApplication.class, args); 
     DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet"); 
     dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.bannerMode(Banner.Mode.OFF).sources(SeliApplication.class); 
    } 

    @Override 
    protected SpringApplicationBuilder createSpringApplicationBuilder() { 
     return new SpringApplicationBuilder().bannerMode(Banner.Mode.OFF); 
    } 
} 

在我的應用程序沒有其他異常處理程序,並拋出後,我得到的消息,我設置Cant add auction

+0

那麼拋出'JdbcExceptions'時究竟發生了什麼?你有沒有登錄控制檯?你的應用程序中是否有其他異常處理程序? –

+0

您可以添加SpringBoot主類和ExceptionHandlingController輸入語句的代碼嗎? – developer

+0

爲彈簧啓動提供您的配置。看來@ControlerAdvice沒有被檢測到。 – ScanQR

回答

0

你可以嘗試添加@EnableWebMvc到您的配置文件

@Configuration 
@EnableWebMvc 
@ComponentScan(...) 
public class MyAppConfiguration { 

}