I have a RESTful API which is supposed to provide file upload service. Several types should be supported by this API such as JSON, XML, and so on. As I want to convert them into the same format to ease the next flow, I write converters for each of types. Is it possible that Spring can autowire the corresponding converter based on the Content-Type
header in the HTTP request, so that I don't need to write the API several times with accepting different Content-Type
?
Asked
Active
Viewed 788 times
0

user1802604
- 396
- 3
- 14
1 Answers
0
As far as I know, autowiring is solved while your Spring application starts, not in response to each http request.
Maybe you can find useful this Spring feature ? How to get access to HTTP header information in Spring MVC REST controller?
By using this, you could easily write some kind of factory in order to implement converters for each type of content you support. It would be the most easily extendable way to do it, in order to support new content types in the future without harming your design
@RequestMapping("/upload")
public void upload(
@RequestHeader("Content-Type") String contentType,
@RequestBody String body) {
Converter converter = ConverterFactory.get(contentType);
MyFormat commonFormat = converter.convert(body);
doWhateverIMustWith(commonFormat);
}

Jorge_B
- 9,712
- 2
- 17
- 22