Question

I need to consume a WebService that outputs XML. In code snippet below:

  1. getResult method can return XML with HTML tags (like <p> tag).
  2. So I'll have to manually transform the XML first before parsing it.
  3. But RestTemplate works fine with other calls. So I don't want to discard it & write manual logic everywhere.

Question:

  • Is there a built-in way to get back raw xml as string using RestTemplate?
  • Do I have to write a custom converter? Any pointers?

Following is my code :

@Rest(rootUrl = "http://my.root.url", converters ={SimpleXmlHttpMessageConverter.class })
public interface MyRestClient {

   @Get("/path/to/restmethod/{day}")
   MyResponse getResult(int day);  <-------------- Returns null when return type is changed to String

}

I tried setting return type to String. But it returns null with this error: Failed to convert value of type 'null' to required type 'java.lang.String'.

Était-ce utile?

La solution

Alright found the answer myself. In addition to changing the return type to String, I also had to add StringHttpMessageConverter as a converter. Snippet below:

@Rest(rootUrl = "http://my.root.url", converters = { StringHttpMessageConverter.class, SimpleXmlHttpMessageConverter.class})

Note: The order of converters is important.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top