2011-02-04 10 views
0

JaxRSを使用してベンダーのAPIとやりとりするクライアントサイドコードを作成しています。私はGETとPOSTを実行するコードを書くことができました。どちらもリクエストボディを持っていません。私は現在、要求本体のみが送信されているPOSTを実行しようとすると問題に遭遇しています。JSON文字列のリクエスト本体で投稿を行うJax-RSクライアント

{"server":{"name":"HelloKitty","imageId":71,"flavorId":1}} 

次は私が使用していますWebクライアントの定義です(Scalaで書かれたが、私は、ヘッダーと権限が正しく行われていることを知っている):私は、単純なJSON文字列を送信しようとしています

ここで

def postClient: WebClient = { 
    val authClient = WebClient.create(authServer) 
    authClient.header("X-Auth-User", apiUsername) 
    authClient.header("X-Auth-Key", apiKey) 

    val response = authClient.get 
    val metadata = response getMetadata() map { case (k, v) => k -> v(0).toString } 

    val client = WebClient.create(metadata.get("X-Server-Management-Url").getOrElse("Error in authentication response")) 
    client.header("X-Auth-User", apiUsername) 
    client.header("X-Auth-Key", apiKey) 
    client.header("X-Auth-Token", metadata.get("X-Auth-Token").getOrElse("Error in authentication response")) 
    client.header("Content-Type","application/json") 
} 
は、私のリソースインタフェースである:ここで
@Produces(Array("text/json")) 
trait RackspaceServerApi { 
    @Path("/servers.json") 
    @GET 
    def listServers(): String 

    @Path("/servers/detail.json") 
    @GET 
    def listServersDetailed(): String 

    @POST 
    @Path("/servers") 
    @Consumes(Array("application/json")) 
    def createServerData(server: String) 
} 

プロキシを作成し、要求を送信しようとする私の方法であり、

def createServer(name: String, imageId: Long, flavorId: Int) = { 
    // this creates a simple pojo for the server 
    val serverObject = new RackspaceCreateServerObject(name, imageId, flavorId, None, None) 

    // i am using lift-web to handle the json 
    // here I am unmarshalling the serverObject into a json string 
    val jsonServerData = write(serverObject) 

    val postProxy = JAXRSClientFactory.fromClient(RackspaceConnection.postClient, classOf[RackspaceServerApi], true) 
    postProxy.createServerData(jsonServerData) 
} 

そして、ここでは私が得る特定のWebApplicationExceptionです:

ステータスコード:[500]
エンティティ:[レスポンスクラスが見つかりませんメッセージ本文ライター:。JAXBElementの]

しかし、もしI次のようにプロキシではなくWebクライアントを使用してリクエストを送信してください。

def createServer(name: String, imageId: Long, flavorId: Int) = { 
    // this creates a simple pojo for the server 
    val serverObject = new RackspaceCreateServerObject(name, imageId, flavorId, None, None) 

    // i am using lift-web to handle the json 
    // here I am unmarshalling the serverObject into a json string 
    val jsonServerData = write(serverObject) 

    val webClient = RackspaceConnection.postClient 
    webClient.path("/servers") 
    webClient.post(jsonServerData) 
} 

これは機能します。インターフェイスで何か間違っているのですか(アノテーションのいくつかの魔法の組み合わせがありませんか?)それとも私は何かを設定しませんでしたか? Jax-RS 2.3.2を使用しています。

答えて

0

どのJAX-RS実装を使用していますか?

エラーメッセージは、JAXB BeanのJSONへのマーシャリングを処理するプラグイン/モジュールを登録する必要があることを示しています。一部のJAX-RS実装ではこれが自動的に行われますが、初期化が必要なものもあれば、

関連する問題