[英]Calling a web service with no wsdl
我想編寫一個Java程序來調用Web服務。 WSDL不適用於此Web服務。 我已經編寫了程序來調用具有wsdl的Web服務。 在這里,我不知道如何進行。 也無法在Internet中找到許多樣本。
我可以使用更好的框架嗎? 我正在從Web服務獲取JSON輸出。
我正在尋找編寫可能的最佳案例的選項(如果我可以編寫一個通用程序,而無需做太多更改就可以將其用於許多Web服務,那就太好了)
好吧,有幾種消耗休息服務的方法。
使用Spring框架:
import org.springframework.web.client.RestTemplate
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject("http://localhost:8080/users/2", User.class);
System.out.println("Username: " + user.getUsername());
使用apache httpclient:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://localhost:8080/users/2");
HttpResponse response = httpClient.execute(getRequest);
HttpEntity httpEntity = response.getEntity();
String userString = EntityUtils.toString(httpEntity);
// Transform 'userString' into object using for example GSON:
Gson gson = new Gson();
User user = gson.fromJson(userString, User.class);
System.out.println("Username: " + user.getUsername());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.