我正在为 Spring Boot 应用程序进行异常处理。我创建了自己的异常类女巫,我正在抛出,一切正常我在控制台中收到异常,但我无法访问我的 @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");
}
}
由于描述为空而引发异常(我这样做只是为了检查我是否会访问我创建的异常处理程序)。
异常类如下所示:
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