Android Annotations + RestTemplate - 将响应xml作为字符串

时间:2014-03-03 02:55:04

标签: android xml xml-parsing resttemplate android-annotations

我需要使用输出XML的WebService。在下面的代码段中:

  1. getResult 方法可以使用HTML标记返回XML(例如< p>标记)。
  2. 所以我必须在解析之前先手动转换XML。
  3. 但RestTemplate可以与其他调用一起使用。所以我不想丢弃它&到处写手册逻辑。
  4. 问题

    • 是否有使用RestTemplate将原始xml作为字符串返回的内置方法?
    • 我是否必须编写自定义转换器?有什么指针吗?

    以下是我的代码:

    @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
    
    }
    

    我尝试将返回类型设置为String。但它返回null并出现此错误:Failed to convert value of type 'null' to required type 'java.lang.String'

1 个答案:

答案 0 :(得分:0)

好吧,我自己找到了答案。除了将返回类型更改为String之外,我还必须将 StringHttpMessageConverter 添加为转换器。下面的代码段:

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

注意: 转换器的顺序很重要。