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

使用 restTemplate 实现get/post 请求的更多相关文章

  1. SpringMVC RestTemplate的几种请求调用

    转载:https://blog.csdn.net/ThinkingLink/article/details/45366777 1.用统一的方法模板进行四种请求:POST,PUT,DELETE,GET ...

  2. RestTemplate进行表单请求,注意要使用MultiValueMap

    在对接API的时候,有时候文档中会说,表单提交,这时候就需要用到 MultiValueMap来操作,下面给大家展示一个简单的demo. MultiValueMap<Object, Object& ...

  3. 精讲RestTemplate第8篇-请求失败自动重试机制

    本文是精讲RestTemplate第8篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  4. RestTemplate远程调用POST请求:HTTP 415 Unsupported Media Type

    这是本项目的接口 称为client @POST @Path("/{urlcode}") @Consumes(MediaTypes.JSON_UTF_8) @Produces(Med ...

  5. restTemplate 发送http post请求带有文件流、参数

    String httpMethod = ""; RestTemplate restTemplate = new RestTemplate(); String args = &quo ...

  6. RestTemplate的三种请求方式

    转载 https://blog.csdn.net/qq_36364521/article/details/84203133

  7. Spring RestTemplate中几种常见的请求方式

    https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...

  8. RestTemplate发送HTTP、HTTPS请求

    RestTemplate 使用总结   场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...

  9. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

随机推荐

  1. Appium环境搭建python篇(mac系统)

    1.安装Appium 通过终端安装: 安装nodejs,下载地址:https://nodejs.org/download/,安装完成后打开终端输入node -v,检查是否安装成功 安装npm,打开终端 ...

  2. 如何避免SHRINKDATABASE & SHRINKFILE 产生索引碎片(转载)

    1. TRUNCATEONLY参数的使用我们在建立的Job中通常使用如下的语法DBCC SHRINKDATABASE (N'DB', 10,TruncateOnly)其中TruncateOnly的用处 ...

  3. sqlserver性能调优中的逻辑读,物理读,预读是什么意思

    表 'T_EPZ_INOUT_ENTRY_DETAIL'.扫描计数 1,逻辑读 4825 次,物理读 6 次,预读 19672 次.SQL SERVER 数据库引擎当遇到一个查询语句时,SQL SER ...

  4. Git钩子详解

    钩子 Git钩子是在Git仓库中特定事件发生时自动运行的脚本.可以定制一些钩子,这些钩子可以在特定的情况下被执行,分为Client端的钩子和Server端的钩子.Client端钩子被operation ...

  5. qt调用sql server存储过程并获取output参数

    最近新做的一个项目需要使用qt5连接另一台机器上的sql server,虽然网上已有类似文章,但还是有些其中很少提及的问题,故在这里汇总下: qt连接sql server可以参考这篇文章: <Q ...

  6. List集合的特有功能

    import java.util.ArrayList; import java.util.List; /** * * List集合的特有功能 * A:添加功能 * void add(int index ...

  7. python使用mechanize模拟登陆新浪邮箱

    mechanize相关知识准备: mechanize.Browser()<br># 设置是否处理HTML http-equiv标头 set_handle_equiv(True)<br ...

  8. HDFS核心设计

    一.HDFS核心设计 数据块(block) 数据块是HDFS上最基本的存储单位 HDFS块默认大小为128M         对块进行抽象会带来的好处 一个小文件的大小可以大于网络中任意一个磁盘的容量 ...

  9. 漏洞扫描--openvas

    操作实例演示 0.登录openvas 点击“openvas start”启动openvas相关服务,服务启动成功之后!在浏览器输入网址:https://127.0.0.1/login/login.ht ...

  10. 团队-UML

    UML设计 分工 刘双玉 李佳铭 杜宏庆 肖小强 汪志彬 江郑 符天愉 邓弘立 后台数据库 求购模块 浏览检索商品 即时聊天系统 商品管理 管理员系统 后台商品发布收藏系统 登录注册与个人信息系统 U ...