您的位置:首页 > 其它

使用RestTemplate实现rest服务的调用

2017-08-25 00:00 841 查看
摘要: 使用spring的restTemplate模板实现远程服务调用。

添加maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

创建rest工具类继承RestTemplate

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;

/**
* Desc:
* Author: 李阳
* mobile: 15002992382
* email:ly_triangle@126.com
* Date: 2017/8/23 16:01
*/
@Component
public class ClientUtil extends RestTemplate {

public JSONObject callGetMethod(String requestUrl) {
HttpEntity entity = new HttpEntity(new HttpHeaders());
ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.GET, entity, String.class);

return JSONObject.parseObject(result.getBody());
}

public JSONObject callPostMethod(String requestUrl, String jsonBody) {
HttpHeaders header = new HttpHeaders();
header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8");
HttpEntity entity = new HttpEntity(jsonBody, header);
ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.POST, entity, String.class);

return JSONObject.parseObject(result.getBody());
}

public JSONObject callPutMethod(String requestUrl, String jsonBody) {
HttpHeaders header = new HttpHeaders();
header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8");
HttpEntity entity = new HttpEntity(jsonBody, header);
ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.PUT, entity, String.class);

return JSONObject.parseObject(result.getBody());
}

public JSONObject callDeleteMethod(String requestUrl) {
HttpHeaders header = new HttpHeaders();
HttpEntity entity = new HttpEntity(header);
ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.DELETE, entity, String.class);

return JSONObject.parseObject(result.getBody());
}

调用服务:

import com.alibaba.fastjson.JSONObject;
import com.utils.ClientUtil;
import com.utils.HttpEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
importorg.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
* Author: 李阳
* Date: 16/08/2017 8:54 PM
* Desc: Rest服务调用
*/
@Service
public class RestServiceInvoking {

@Autowired
private ClientUtil clientUtil;

public JSONObject loadSth(Long id) {
String requestUrl = tmisServiceUrl + "/sth/" + id;
ResponseEntity<String> result = clientUtil.callGetMethod(requestUrl);
return JSONObject.parseObject(result.getBody());
}

public JSONObject addSth(String view) {
String requestUrl = tmisServiceUrl + "/sth";
ResponseEntity<String> result =  clientUtil.callPostMethod(requestUrl,view);

return JSONObject.parseObject(result.getBody());
}

public JSONObject updateSth(String view) {
String requestUrl = tmisServiceUrl + "/sth";
ResponseEntity<String> result =  clientUtil.callPutMethod(requestUrl,view);

return JSONObject.parseObject(result.getBody());
}

public JSONObject deleteSth(Long id) {
String requestUrl = tmisServiceUrl + "/sth/" + id;
ResponseEntity<String> result =  clientUtil.callDeleteMethod(requestUrl);

return JSONObject.parseObject(result.getBody());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐