2017-02-15 14 views
1

次のシナリオがあります。POSTする方法RestTemplateを使用してXML文字列

クエリ-users.xmlの

<?xml version="1.0"?> 
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"> 
</q:query> 

カール称賛を実行する:私はXMLで所望の応答を得る

curl.exe --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST http://localhost:8080/midpoint/ws/rest/users/search -d @C:\Users\user\query-users.xml 

私は、XMLファイルを持っています。 私は、JavaコードからRestTemplateを使用して同じPOSTリクエストをやろうとしています:

try{  
    StringBuilder builder = new StringBuilder(); 
    builder.append("http://localhost:8080/midpoint/ws/rest/users/search"); 
    builder.append("?query="); 
    builder.append(URLEncoder.encode("<?xml version=\"1.0\"?><q:query xmlns:q=\"http://prism.evolveum.com/xml/ns/public/query-3\"></q:query>")); 

    URI uri = URI.create(builder.toString()); 

    restOperations.postForEntity(uri, new HttpEntity<String>(createHeaders("username", "pass")), String.class); 
    logger.info(response); 
    }catch(Exception e){ 
     logger.info(e.getMessage()); 
    } 
} 

私はInternal Servel Errorを取得します。 XMLの文字列をRestTemplateというPOSTリクエストに渡して間違っていることがありますが、それが何であるのか分かりません。

これを解決する方法はありますか?

ありがとうございました

+0

は、サーバーのログへのアクセス権を持っていますか?私はあなたがローカルホストにアクセスしているのを見ています – TungstenX

+2

あなたはリクエストボディにあなたのXMLを投稿していません、あなたはクエリパラメ...この投稿はあなたを助けるかもしれませんhttp://stackoverflow.com/questions/35461148/how- to-send-xml-post-requests-with-spring-resttemplate – anders

答えて

0

あなたのcurl呼び出しとRestTemplate呼び出しは同じではありません。最初は、xmlをHTTP Requestの本体として渡します(これは-dオプションと同じです)。 RestTemplateでは、xmlをクエリに割り当てます。その結果、HTTPリクエストにペイロードはなく、データはURLにエンコードされます。

あなたはHTTPボディとしてあなたのxmlを渡したい場合は、別のHttpEntityのconstuctorを使用する必要があります。http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpEntity.html#HttpEntity-T-org.springframework.util.MultiValueMap-

関連する問題