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"; } } } }