в приложении REST API, методом обработки обработки исключений должен быть реагированием или аннотировать метод с помощью @ResponseBody , чтобы Spring Boot мог выполнять сериализацию HTTP. < /p>
Обновление < /strong> < /p>
класс стартера: < /p>
Код: Выделить всё
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@EnableTransactionManagement//TODO remove this line if not needed
public class Application extends SpringBootServletInitializer{
private static Class applicationClass= Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(applicationClass);
}
}
Код: Выделить всё
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestEntity handleException(HttpServletRequest req, RuntimeException ex) {
RestEntity restEntity=new RestEntity();
Message message=new Message();
message.setCode(1000);
message.setMessage("Something wrong with the server");
restEntity.setMessage(message);
return restEntity;
}
}
Оказывается, что это не работает. Я что -то упускаю здесь? Я не почему ... < /p>
Кто -нибудь может помочь? < /P>
Подробнее здесь: https://stackoverflow.com/questions/303 ... out-having
Мобильная версия