在RESTful API中提取XML响应而不是JSON

时间:2014-12-10 02:40:24

标签: xml json rest soap groovy

我目前正在使用这个REST API http://pokeapi.co作为我的Groovy-REST实践。现在,我可以使用SOAP UI 5.0从REST响应中获取特定的JSON数据,并从中进行简单的输出。 SOAP UI还生成JSON响应的XML版本。我想知道的是,是否可以使用返回的XML而不是JSON数据?如果是这样,怎么样?我已经浏览了几个小时如何做但没有运气

这是我目前的代码:

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')

import wslite.rest.*

class PokeApi {
    def restUrl = "http://pokeapi.co"
    def client = new RESTClient(restUrl)

    def getGameVer(def ver){
        def response = client.get(path: '/api/v1/game/' +ver+ '/')
        def result = ('Version Name: ' + response.json.name
                + '\nRelease Year: ' + response.json.release_year
                + '\nGame Generation: ' + response.json.generation
                )
        return result
    }

    def getPokemonInfo(def pokeID){
        def response = client.get(path: '/api/v1/pokemon/'+pokeID+'/')

        def pokeName = response.json.name
        def pokeNatID = response.json.national_id
        def ability1 = response.json.abilities[0].name
        def ability2 = response.json.abilities[1].name

        def result = ("Pokemon name: " + pokeName
                +"\nNationalID: " + pokeNatID
                +"\nAbility 1: " +ability1
                +"\nAbility 2: " + ability2)

        return result
    }



    static void main(String[] args){
        def poke = new PokeApi()
        def abc = poke.getGameVer(2)
        def bcd = poke.getPokemonInfo(399)
        println abc
        println bcd
    }
 }

0 个答案:

没有答案