Java 未到达Spring boot@ExceptionHandler
我正在为spring boot应用程序处理异常。我已经创建了我自己的异常类,我正在抛出它,一切正常,我在控制台中得到异常,但我无法访问@ExceptionHandler方法 引发异常的我的类:Java 未到达Spring boot@ExceptionHandler,java,spring-mvc,spring-boot,exception-handling,throwable,Java,Spring Mvc,Spring Boot,Exception Handling,Throwable,我正在为spring boot应用程序处理异常。我已经创建了我自己的异常类,我正在抛出它,一切正常,我在控制台中得到异常,但我无法访问@ExceptionHandler方法 引发异常的我的类: @Override public AuctionBody insertNewAuction(AuctionBody auctionBody, int ownerId, String AUCTION_TYPE) { try { SimpleJdbcInsert simpleJdbcI
@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);
}
}
在我的应用程序中没有其他异常处理程序,在抛出之后,我得到了我设置的消息
不能添加拍卖您可以尝试将@EnableWebMvc添加到配置文件中
@Configuration
@EnableWebMvc
@ComponentScan(...)
public class MyAppConfiguration {
}
您可以尝试将@EnableWebMvc添加到配置文件中
@Configuration
@EnableWebMvc
@ComponentScan(...)
public class MyAppConfiguration {
}
如果将来有人遇到此问题,请尝试设置annotations
属性,即
@RestControllerAdvice(annotations = [RestController::class]) // without `[]` if you're not using kotlin
class SomeExceptionHandler
如果将来有人遇到此问题,请尝试设置annotations
属性,即
@RestControllerAdvice(annotations = [RestController::class]) // without `[]` if you're not using kotlin
class SomeExceptionHandler
那么当抛出jdbceptions
时到底发生了什么呢?您在控制台中有任何日志吗?您的应用程序中是否有其他异常处理程序?能否添加SpringBoot主类和ExceptionHandlingController的代码,包括导入语句?提供spring boot的配置。似乎未检测到@ControlerAdvice。@TechBreak但如果未检测到它,它是否会调用ExceptionHandlingController
构造函数。我想您缺少异常处理程序解析程序?这可能会有帮助。那么当抛出jdbceptions
时到底发生了什么呢?您在控制台中有任何日志吗?您的应用程序中是否有其他异常处理程序?能否添加SpringBoot主类和ExceptionHandlingController的代码,包括导入语句?提供spring boot的配置。似乎未检测到@ControlerAdvice。@TechBreak但如果未检测到它,它是否会调用ExceptionHandlingController
构造函数。我想您缺少异常处理程序解析程序?这可能会有帮助。