From b09fad13a1f9517abd901587c9299bf5b65ed061 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Mon, 23 Jul 2018 10:35:30 +0200 Subject: [PATCH] Catch errors when adding SourceHttpMessageConverter This commit ignores errors like TransformerFactoryConfigurationError that can be thrown when instantiating SourceHttpMessageConverter on platforms where no TransformerFactory implementation is available, like when compiling/running as GraalVM native images. Issue: SPR-17007 --- .../support/AllEncompassingFormHttpMessageConverter.java | 9 +++++++-- .../org/springframework/web/client/RestTemplate.java | 7 ++++++- .../config/annotation/WebMvcConfigurationSupport.java | 7 ++++++- .../annotation/ExceptionHandlerExceptionResolver.java | 7 ++++++- .../method/annotation/RequestMappingHandlerAdapter.java | 7 ++++++- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java index 7d68850edf..07416cba2e 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,7 +64,12 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv public AllEncompassingFormHttpMessageConverter() { - addPartConverter(new SourceHttpMessageConverter<>()); + try { + addPartConverter(new SourceHttpMessageConverter<>()); + } + catch (Error err) { + // Ignore when no TransformerFactory implementation is available + } if (jaxb2Present && !jackson2XmlPresent) { addPartConverter(new Jaxb2RootElementHttpMessageConverter()); diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 3760750e79..eb4407784d 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -179,7 +179,12 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new ResourceHttpMessageConverter(false)); - this.messageConverters.add(new SourceHttpMessageConverter<>()); + try { + this.messageConverters.add(new SourceHttpMessageConverter<>()); + } + catch (Error err) { + // Ignore when no TransformerFactory implementation is available + } this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 6de372c62c..5d2b5ddae7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -805,7 +805,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv messageConverters.add(stringHttpMessageConverter); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new ResourceRegionHttpMessageConverter()); - messageConverters.add(new SourceHttpMessageConverter<>()); + try { + messageConverters.add(new SourceHttpMessageConverter<>()); + } + catch (Error err) { + // Ignore when no TransformerFactory implementation is available + } messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java index 7f49e3635a..09e4c7b6f2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java @@ -111,7 +111,12 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce this.messageConverters = new ArrayList<>(); this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(stringHttpMessageConverter); - this.messageConverters.add(new SourceHttpMessageConverter<>()); + try { + this.messageConverters.add(new SourceHttpMessageConverter<>()); + } + catch (Error err) { + // Ignore when no TransformerFactory implementation is available + } this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index be4ad2a854..99bbcc3dd8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -184,7 +184,12 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter this.messageConverters = new ArrayList<>(4); this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(stringHttpMessageConverter); - this.messageConverters.add(new SourceHttpMessageConverter<>()); + try { + this.messageConverters.add(new SourceHttpMessageConverter<>()); + } + catch (Error err) { + // Ignore when no TransformerFactory implementation is available + } this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); }