在没有 wsdl 的情况下调用 Web 服务 [英] Calling a web service with no wsdl

查看:38
本文介绍了在没有 wsdl 的情况下调用 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 Java 程序来调用 Web 服务.WSDL 不适用于此 Web 服务.我编写了程序来调用具有 wsdl 的 Web 服务.在这里,我不知道如何继续.也无法在互联网上找到很多样本.

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.

有什么更好的框架可以使用吗?我正在从 Web 服务获取 JSON 输出.

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)

推荐答案

嗯,有几种方式来消费休息服务.

Well there are several ways to consume rest service.

使用 Spring 框架:

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

使用 apache httpclient:

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

这篇关于在没有 wsdl 的情况下调用 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆