0

私は Play フレームワーク ドキュメントを解析し、指定されたドメイン オブジェクトから XML 応答を生成するためにすぐに使用できるものがあるかどうかを調べようとしていJson.toJson(Object)ます。

次のコードは、play フレームワーク 2.1.2 の Json REST API で正常に動作します。Json の代わりに、ここで箱から出してすぐに XML を生成する方法を提案できますか?

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 に答える