import de.juplo.thymeleaf.JuploDialect;
import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.apache.http.HttpStatus;
+import java.util.Properties;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
-import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
+import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
-import org.springframework.http.MediaType;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.web.servlet.View;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;
-import org.thymeleaf.exceptions.TemplateInputException;
import org.thymeleaf.resourceresolver.IResourceResolver;
import org.thymeleaf.resourceresolver.UrlResourceResolver;
-import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.TemplateResolver;
}
@Bean
- public View error(ThymeleafViewResolver resolver) throws Exception
+ public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
{
- return resolver.resolveViewName("templates/500", Locale.getDefault());
+ SimpleMappingExceptionResolver resolver =
+ new SimpleMappingExceptionResolver();
+
+ Properties mappings = new Properties();
+ mappings.setProperty("TemplateInputException", "templates/404");
+
+ resolver.setExceptionMappings(mappings);
+ resolver.setDefaultErrorView("templates/500");
+ resolver.setWarnLogCategory("exception");
+ return resolver;
}
@Bean
- public ErrorController errorController(ErrorAttributes errorAttributes)
+ public ErrorController errorController(
+ ApplicationContext context,
+ ErrorAttributes errorAttributes
+ )
{
- return new CustomErrorController(errorAttributes, properties.getError());
+ return new ExceptionResolverErrorController(
+ context,
+ errorAttributes,
+ properties.getError()
+ );
}
{
SpringApplication.run(Application.class, args);
}
-
-
- static class CustomErrorController extends BasicErrorController
- {
- public final static String TEMPLATE_INPUT_EXCEPTION =
- TemplateInputException.class.getCanonicalName();
-
-
- CustomErrorController(
- ErrorAttributes errorAttributes,
- ErrorProperties errorProperties
- )
- {
- super(errorAttributes, errorProperties);
- }
-
-
- @Override
- public ModelAndView errorHtml(
- HttpServletRequest request,
- HttpServletResponse response
- )
- {
- Map<String, Object> model =
- getErrorAttributes(
- request,
- isIncludeStackTrace(request, MediaType.TEXT_HTML)
- );
-
- String view;
-
- switch ((String)model.get("exception"))
- {
- case "org.thymeleaf.exceptions.TemplateInputException":
- response.setStatus(HttpStatus.SC_NOT_FOUND);
- view = "templates/404";
- break;
-
- default:
- response.setStatus(getStatus(request).value());
- view = "templates/500";
- break;
- }
-
- return new ModelAndView(view, model);
- }
- }
}
\ No newline at end of file