• 使用 restTemplate 实现get/post 请求


    get 请求(这里是在 idea 的 test包中,所以需要直接 new RestTemplate() )

    即:RestTemplate restTemplate = new RestTemplate();

    package org.darkblue.monitor;
    
    import org.junit.Test;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    public class TestTemplate {
    
        @Test
        public void test() {
            RestTemplate restTemplate = new RestTemplate();
    
            HttpHeaders headers = new HttpHeaders();
            MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
            String str = restTemplate.postForObject("http://192.168.1.15/DETECT-X/", requestEntity, String.class);
    
            System.out.println(str.toString());
    
        }
    
    }

    post 请求(这里是在非test包中,所以可以自动注入):

    package org.darkblue.monitor.controller;
    
    import com.kinome.surveillance.web.dao.RepoDao;
    import com.kinome.surveillance.web.dao.RepoDaoImp;
    import com.kinome.surveillance.web.entity.RepoEntity;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.client.RestTemplate;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.List;
    
    
    @Controller
    public class CensorStatusController {
    
        @Autowired
        HttpServletRequest request;
        @Autowired
        RestTemplate restTemplate;
    
        @ResponseBody
        @RequestMapping("updateCensorStatus")
        public String updateCensorStatus(RepoEntity re) {
    
            RepoDao repoDao = new RepoDaoImp();
            RepoEntity repoEntity = new RepoEntity();
            int intCensorStatus = re.getCensorStatus();
            int intRid = re.getRid();
            System.out.println("intRid =========> " + intRid);
            List<RepoEntity> repoByRid = repoDao.getRepoByRid(intRid);
            for (RepoEntity rbr :
                    repoByRid) {
                repoEntity.setPtLink(rbr.getPtLink());
                repoEntity.setRemark(rbr.getRemark());
            }
    
            if (re.getCensorStatus() == 2) {
                repoDao.deleteRid(intRid);
    
                int userId = (int) request.getSession().getAttribute("userId");
                HttpHeaders headers = new HttpHeaders();
                headers.add("X-Auth-Token", "e348bc22-5efa-4299-9142-529f07a18ac9");
    
                MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
                postParameters.add("item0_1", repoEntity.getPtLink());
                postParameters.add("item0_2", repoEntity.getRemark());
                postParameters.add("userid", String.valueOf(userId));
    
                HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
    
                String str = restTemplate.postForObject("http://localhost:8080/DETECT-X/BulkAddPtLinkAndRemark.action", requestEntity, String.class);
    //        System.out.println(str.toString());
                return "1";
            } else {
                if (repoDao.setCensorStatusByRid(intCensorStatus, intRid)) {
                    return "1";
                } else {
                    return "0";
                }
            }
    
    
        }
    
    }
  • 相关阅读:
    array_values — 返回数组中所有的值
    array_unshift — 在数组开头插入一个或多个单元
    array_unique — 移除数组中重复的值
    array_uintersect — 计算数组的交集,用回调函数比较数据
    array_uintersect_uassoc — 带索引检查计算数组的交集,用单独的回调函数比较数据和索引
    职场中的起跑线上,从赢在一个办公邮箱开始
    可终身使用的会员邮箱靓号,到底有多酷?
    商务业务人员,用什么邮箱更能获得认可?
    电子邮箱有哪些类型,2020什么邮箱最火爆?
    外贸邮箱用哪个比较好?企业域名邮箱注册哪个好?
  • 原文地址:https://www.cnblogs.com/kinome/p/10494831.html
Copyright © 2020-2023  润新知