I am working on exception handling for spring boot application. I have created my own Exception class witch I am throwing, everything is working fine I get exception in console but I can't reach my @ExceptionHandler method.
My class that throws 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 is thrown because of description being null (I did it just to check if i'll get to my created exception handler or not).
The Exception class looks like this:
public class JdbcExceptions extends RuntimeException {
public JdbcExceptions(String message) {
super(message);
}
}
And the exception handler controller class looks like this:
@ControllerAdvice
public class ExceptionHandlingController {
@ExceptionHandler(JdbcExceptions.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String getJdbcException(JdbcExceptions ex){
return "redirect:/html/errorPage";
}
}
I know that it should work and I am sure that I have some kind of bad configuration that is making my @ExceptionHandler unreachable, but I haven't found the answer. Also ExceptionHandlingController class is created on application run.
Main class:
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);
}
}
No other Exception handler in my application and after throw I get that message that I set Cant add auction