Знание поэтому я подумал, что было бы неплохо установить для этого параметра значение true.
Я использую Spring Boot.
Сделал так:
MyConfig.java
Код: Выделить всё
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
@SpringBootApplication
public class MyConfig {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MyConfig.class, args);
context.getBean(DispatcherServlet.class).setThrowExceptionIfNoHandlerFound(true);
// ...
}
}
CustomExceptionHandler.java (это не сработало)< /p>
Код: Выделить всё
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// do my own handle ...
// then, return ...
}
}
CustomExceptionHandler.java (это сработало)
Код: Выделить всё
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@EnableWebMvc
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// do my own handle ...
// then, return ...
}
}
"Если вы хотите получить полный контроль над Spring MVC, вы можете добавить свою собственную @Configuration с аннотацией @EnableWebMvc. ."
Потерю ли я автоконфигурацию Spring MVC? Я имею в виду @EnableWebMvc, в моем случае это не класс @Configuration.
Мой вопрос основан на том факте, что Я не уверен, как работает приведенный выше код, и хочу знать, есть ли другой способ сделать это без потери автоматической конфигурации Spring MVC. Может кто-нибудь объяснить?
Подробнее здесь: https://stackoverflow.com/questions/510 ... dexception
Мобильная версия