0

我正在解析 Play 框架文档,并试图确定是否有任何现成的东西可用于从给定的域对象生成 XML 响应,就像我们为Json.toJson(Object).

以下代码适用于 play framework 2.1.2 中的 Json REST API,任何人都可以建议如何在此处开箱即用地生成 XML 而不是 Json?

package controllers;

import java.util.List;
import java.util.concurrent.Callable;

import play.Logger;
import play.libs.F.Function;
import play.libs.F.Promise;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;

import com.amazonaws.services.simpledb.model.Item;

public class ShowItemsJson extends Controller {

    public static Result allItems() {
        // Now create the async process to lookup items in simpledb
        AllItems<List<Item>> callable = new AllItems<List<Item>>();
        Promise<List<Item>> promise = play.libs.Akka.future(callable);
        return async(promise.map(new Function<List<Item>, Result>() {
            public Result apply(List<Item> rm) throws Throwable {
                // Convert the result into json before sending.
                // TODO How to do same for XML?
                return ok(Json.toJson(rm));
            }
        }));
    }

    // One instance of this class should be used for each create request
    static class AllItems<V> implements Callable<V> {

        @SuppressWarnings("unchecked")
        public V call() throws Exception {
            try {
                return (V) Test.getAllItems();
            } catch (Error e) {
                // Error is handled here to log NoClassDefFoundError
                Logger.error("Error: ", e);
                throw e;
            }
        }
    }
}
4

1 回答 1

0

据我所知,Play 2 中没有内置支持从 Java 对象生成 XML,但 Java 领域有很多选项。

仅举几例:

于 2013-08-07T07:11:04.103 回答