RestTemplate的使用

一、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;
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值