1

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

6
  • So what exactly is happening when the JdbcExceptions is thrown? Do you have any log in the console? Do you have any other exception handlers in your application? Commented Nov 18, 2016 at 12:33
  • Can you add the code for SpringBoot main class and ExceptionHandlingController inlcuding import statements ? Commented Nov 18, 2016 at 12:34
  • provide your configuration for spring boot. It seems that @ControlerAdvice is not detected. Commented Nov 18, 2016 at 12:49
  • @TechBreak but if it wouldn't be detected would it call ExceptionHandlingController constructor. Commented Nov 18, 2016 at 12:57
  • I think you are missing exception handler resolver? This might help. stackoverflow.com/questions/19498378/… Commented Nov 18, 2016 at 13:02

3 Answers 3

0

You can try adding @EnableWebMvc to your config file

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

 }
Sign up to request clarification or add additional context in comments.

Comments

0

In case anyone encounters this problem in the future, try setting up annotations property, that is

@RestControllerAdvice(annotations = [RestController::class])  // without `[]` if you're not using kotlin
class SomeExceptionHandler

Comments

0

If you can't find solution for error "@ExceptionHandler cannot be reached with java8+" maybe you made same mistake as I did. I named my exception handler class as "ExceptionHandler" which is same as name of annotation @ExceptionHandler. After correction, my IDE was able to import as:

"org.springframework.web.bind.annotation.ExceptionHandler;"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.