2016-11-18 4 views
1

Je travaille sur la gestion des exceptions pour les applications de démarrage à ressort. J'ai créé ma propre classe d'Exception que je lance, tout fonctionne correctement, j'ai une exception dans la console mais je n'arrive pas à atteindre ma méthode @ExceptionHandler.Spring boot @ExceptionHandler n'est pas atteint

Ma classe qui génère une exception:

@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 est levée en raison de la description nulle être (je l'ai fait juste pour vérifier si je vais à mon gestionnaire d'exception créé ou non).

La classe d'exception ressemble à ceci:

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

}

Et ressemble à la classe de contrôleur de gestionnaire d'exception ceci:

@ControllerAdvice 
public class ExceptionHandlingController { 

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

Je sais que cela devrait fonctionner et je suis sûr que j'ai une sorte de mauvaise configuration qui rend mon @ExceptionHandler inaccessible, mais je n'ai pas trouvé la réponse. La classe ExceptionHandlingController est également créée lors de l'exécution de l'application.

Classe principale:

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

Aucun autre gestionnaire d'exception dans ma demande et après un jet je reçois ce message que je mis Cant add auction

+0

Alors, que se passe-t-il exactement lorsque le 'JdbcExceptions' est lancé? Avez-vous un journal dans la console? Avez-vous d'autres gestionnaires d'exceptions dans votre application? –

+0

Pouvez-vous ajouter le code pour la classe principale SpringBoot et ExceptionHandlingController en incluant les instructions d'importation? – developer

+0

fournir votre configuration pour le démarrage de printemps. Il semble que @ControlerAdvice n'est pas détecté. – ScanQR

Répondre

0

Vous pouvez essayer d'ajouter @EnableWebMvc à votre fichier de configuration

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

}