Calling a web service with no wsdl

我是研究僧i 提交于 2019-12-12 01:46:09

问题


I want to write a Java program to call a web service. WSDL is not available for this web service. I have written programs to call a web service which has wsdl. Here I don't have any idea of how I can proceed. Not able to find many samples in Internet as well.

Is there any better frame work which I can use? I am getting JSON output from web service.

I am looking at options of writing a best possible case(If I could write a generalized program which could be used for many web services with out much changes, it would be great)


回答1:


Well there are several ways to consume rest service.

Using Spring framework:

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());

Using 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());


来源:https://stackoverflow.com/questions/25087071/calling-a-web-service-with-no-wsdl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!