Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide alternative RestTemplate constructor to avoid default MessageConverter creation [SPR-11351] #15976

Closed
spring-projects-issues opened this issue Jan 23, 2014 · 6 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 23, 2014

Kek opened SPR-11351 and commented

The default MessageConverters are initialized in constructor of RestTemplate. This solution has some problems:

  1. When I want different set of MessageConverters, the default MessageConverters are initialized first and than replaced by setMessageConverters(..) method. This slows down the application startup.

  2. When I have some classes as JAXB, Jackson on classpath, than some other default converters are initialized too (but I don't use these technologies (JAXB, Jackson) with RestTempate) - this slows down the startup, so I want to specify my "limited" set of Converters - but is the same problem as 1).

  3. When I have 2 WAR applications in Tomcat, and one application sets the System property "javax.xml.transform.TransformerFactory" for Xalan XSLTC, and the second application does not have Xalan on classpath and does not use the XSLT,but instantiates RestTemplate for JSON based communication, than Exception is thrown:

Caused by: javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.xsltc.trax.TransformerFactoryImpl not found
	at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:107)
	at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.<init>(AbstractXmlHttpMessageConverter.java:47)
	at org.springframework.http.converter.xml.SourceHttpMessageConverter.<init>(SourceHttpMessageConverter.java:47)
	at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:154)

I don´t want to initialize any default XML message Converter for RestTemplate, because I use only JSON for communication -> I want to configure my own set of MessageConverters, but the constructor is called first and destroyed by the exception.

So please, move the initialization of default MessageConvertors for RestTemplate from constructor to some @PostConstruct method, and initialize these default converters only in situations, when the Array of converters was not set (preinitialized) by setMessageConventers(...) operation from configuration.


Affects: 3.2.4, 4.0 GA

Issue Links:

Referenced from: commits 1cb9b9c, 425e5a0

Backported to: 3.2.7

0 votes, 8 watchers

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Just so you are aware, Spring Boot provides an HttpMessageConverters convenience @Bean which you can use to grab a global list of message converters to initialize your RestTemplate and MVC stuff. We could copy that in Spring and make the user install that bean himself, I guess. Or we could recommend the use of Spring Boot (probably better for everyone).

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

The RestTemplate can be instantiated directly, not necessarily through Spring configuration so @PostContruct callbacks won't apply there. We could introduce additional constructors accepting a list of converters. Arjen Poutsma, any other suggestions?

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Rossen Stoyanchev that constructor sounds like a good idea.

@spring-projects-issues
Copy link
Collaborator Author

Brian Clozel commented

Resolved on master with change 425e5a06.
Juergen Hoeller, this is ready for backport on the 3.2.x branch.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Looks good, Brian! Backported to 3.2.x as well now.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Kek commented

Thank you very much for the quick fix, great job!

@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.0.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants