0

I am new to Spring Rest,I've tried to fetch xml from webservice, but it's throwing InvalidMediaTypeException after the execution below code:

restTemplate.getForObject("http://www.dictionaryapi.com/api/v1/references/learners/xml/{word}?key={key}",String.class,uriVariables);

The WebService returns xml document, and I thought firstly that it could be the problem, but another WebService (that url doesn't contain 'xml' as path) works great.

Stacktrace:

Exception in thread "main" org.springframework.http.InvalidMediaTypeException: Invalid mime type "xml": does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:385)
at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:722)
at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:114)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:85)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:384)

So how can I exchange data with WebServices, that contain word "xml" in their url path.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • I think, but I might be wrong, that the stack you get is not caused by the "xml" in the URI, can we see the "webService" method? –  Sep 23 '16 at 11:34
  • suddenly , it's a third party WebService, so if I make a request on http://www.dictionaryapi.com/api/v1/references/learners/xml/apple?key=4d0a2da0-791e-4616-986f-2b26da530f04, I receive this results, for example the request on "http://www.thomas-bayer.com/sqlrest/CUSTOMER/" is valid. – Vlad Kudryavtsev Sep 23 '16 at 11:40
  • xml in url parameter may not be the problem here. There should be some problem in service or data you are passing in uri variables – Nimesh Sep 23 '16 at 12:51
  • I've found the answer and it was a mistake in "Media Type". The provider of this 3-d party WebService installed type "xml", but "application/xml" or "text/xml" . So thank you very much for all answers. – Vlad Kudryavtsev Sep 27 '16 at 06:43

1 Answers1

1

The problem here is that service is returning the Response to your query with a messed-up Content-Type, specifically xml. You need to configure Spring to handle that because sane people (and services) use application/xml instead. Check out this answer for custom MediaType settings.

Community
  • 1
  • 1
rorschach
  • 2,871
  • 1
  • 17
  • 20