简体   繁体   English

Spring boot @ExceptionHandler 未达到

[英]Spring boot @ExceptionHandler is not reached

I am working on exception handling for spring boot application.我正在为 Spring Boot 应用程序进行异常处理。 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.我创建了自己的异常类女巫,我正在抛出,一切正常我在控制台中收到异常,但我无法访问我的 @ExceptionHandler 方法。

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.我知道它应该可以工作,并且我确信我有某种错误的配置导致我的 @ExceptionHandler 无法访问,但我还没有找到答案。 Also ExceptionHandlingController class is created on application run. ExceptionHandlingController 类也是在应用程序运行时创建的。

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我的应用程序中没有其他异常处理程序,并且在抛出后我收到我设置Cant add auction消息

You can try adding @EnableWebMvc to your config file 您可以尝试将@EnableWebMvc添加到配置文件中

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

 }

In case anyone encounters this problem in the future, try setting up annotations property, that is 如果将来遇到此问题,请尝试设置annotations属性,即

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

If you can't find solution for error "@ExceptionHandler cannot be reached with java8+" maybe you made same mistake as I did.如果您找不到错误“@ExceptionHandler 无法使用 java8+”的解决方案,那么您可能犯了和我一样的错误。 I named my exception handler class as "ExceptionHandler" which is same as name of annotation @ExceptionHandler.我将我的异常处理程序类命名为“ExceptionHandler”,它与注释 @ExceptionHandler 的名称相同。 After correction, my IDE was able to import as "org.springframework.web.bind.annotation.ExceptionHandler;"更正后,我的 IDE 能够导入为“org.springframework.web.bind.annotation.ExceptionHandler;”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM