一、Get方式的使用
@Value("${url}")
private String url;
public JSONObject photourl(String sfzh, String loginName) {
String photourl = CommUtil.url(url, Constant.CK_PHOTOURL, Constant.CKSERVICE, loginName) + "&sfzh=" + sfzh;
String backInfo = restTemplate.getForObject(photourl, String.class);
JSONObject jsonObject = JSONObject.parseObject(backInfo);
return jsonObject;
}
package com.iflytek.zdrgk.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.util.Calendar;
import java.util.Date;
/**
* @author songmo
* @Description: 请求工具
* @Param
* @return
* @date 2020/7/14 15:27
*/
public class CommUtil {
/**
* @return java.lang.String
* @Description: 请求url封装
* @author songmo
* @Param [url, serviceName, loginName]
* @date 2020/7/14 15:30
*/
public static String url(final String url, final String interfaces, final String serviceName, final String loginName) {
String appurl = url + "?url=" + interfaces + "&serviceName=" + serviceName + "&loginName=" + loginName;
return appurl;
}
/**
* @Description: 请求url封装
* @author jbcao2
*/
public static String urlForDictionaryTable(final String url,
final String interfaces,
final String serviceName,
final String dictionaryType,
final String loginName){
String urlForDictionaryTable = url +
"?url=" + interfaces +
"&serviceName=" + serviceName +
"&zdlb=" + dictionaryType +
"&loginName=" + loginName;
return urlForDictionaryTable;
}
/**
* @return org.springframework.http.HttpHeaders
* @Description: 请求头设置
* @author songmo
* @Param []
* @date 2020/7/14 15:45
*/
public static HttpHeaders headers() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
/**
* @Description: 请求头设置内容类型
* @return org.springframework.http.HttpHeaders
* @author jbcao2
*/
public static HttpHeaders HttpHeadersSetContentTypeApplicationJsonUtf8(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
return httpHeaders;
}
}
public class Constant {
public static final String SERVICENAME="serviceName";
//请求参数
public static final String CKSERVICE="ckservice";
public static final String ZDRSERVICES="zdrservices";
public static final String LOGIN_NAME="loginName";
public static final String SQGKSERVICE="sqgkservice";
public static final String LKSERVICE="lkservice";
}
二、POST传JSON方式
public JSONObject cklsyjlist(String dtxxlb,int pageIndex,int pageSize,String qsfkzt,String sfzh,String yjxxbh,
String keywords, String mbfx,String czcs,String czjg,String czxl,
String czstartTime,String czendTime,String loginName) {
String lsyjUrl = CommUtil.url(this.url, Constant.CK_LSYJLIST, Constant.CKSERVICE, loginName);
//数据封装
JSONObject json = new JSONObject();
json.put("dtxxlb", dtxxlb);
json.put("pageIndex", pageIndex);
json.put("pageSize", pageSize);
json.put("qsfkzt", qsfkzt);
json.put("sfzh", sfzh);
json.put("yjxxbh", yjxxbh);
json.put("autoCount", true);
Map<String,Object> map =new HashMap<String, Object>();
map.put("keywords", keywords);
map.put("mbfx", mbfx);
map.put("czcs", czcs);
map.put("czjg", czjg);
map.put("czxl", czxl);
map.put("czstartTime", czstartTime);
map.put("czendTime", czendTime);
json.put("map",map);
//设置请求类型
HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), CommUtil.headers());
//调用请求
JSONObject jsonObject = restTemplate.postForEntity(lsyjUrl, formEntity, JSONObject.class).getBody();
return jsonObject;
}