微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webService客户端 spring MVC实现

获得

即springMVC。

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.0.6.RELEASE</version>
</dependency>


RestTemplate

org.springframework.web.client.RestTemplate

springMVC的RestTemplate类简化了对webService的调用过程,使用起来非常简便。

<T> T org.springframework.web.client.RestTemplate.getForObject(String url,Class<T> responseType,Object... urlvariables) throws RestClientException
根据指定的url,以get请求获取数据并转换为指定类型的对象。
<T> T org.springframework.web.client.RestTemplate.postForObject(String url,Object request,Object... uriVariables) throws RestClientException
根据指定的url,以post请求获取数据并转换为指定类型的对象。

示例

package com.likeyichu.webservice.client;

import org.springframework.web.client.RestTemplate;

import com.likeyichu.webservice.resource.Student;

public class MyClient {

	public static void main(String[] args) {
		RestTemplate client=new RestTemplate();
		String url="http://localhost:8080/WebService/student";
		Student student=client.getForObject(url,Student.class);
		System.out.println(student.getName());
	}

}
//xiaoMing

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐