restTemplate发送携带header参数的get和post请求demo

简介: restTemplate发送携带header参数的get和post请求demo

service


package io.wz.service;
import com.google.gson.Gson;
import io.wz.pojo.req.guest.GuestInfo;
import io.wz.pojo.req.guest.GuestRecord;
import io.wz.pojo.resp.guest.GuestResp;
import io.wz.pojo.resp.guest.GuestTokenResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
 * 推送数据
 */
@Service
public class GuestMachineService {
    @Value("${guest.url}")
    private String url;
    @Value("${guest.loginName}")
    private String loginName;
    @Value("${guest.loginPassWord}")
    private String loginPassWord;
    private final String PARAM_TOKEN = "token";
    private final String PARAM_SOURCE = "source";
    private final String VALUE_SOURCE = "fk_server";
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 获取token
     * https://blog.csdn.net/qq_26702601/article/details/91864118
     */
    public GuestTokenResp getAccessToken() {
        HttpHeaders headers = new HttpHeaders();
        headers.add(PARAM_SOURCE, VALUE_SOURCE);
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        ResponseEntity<GuestTokenResp> exchange = restTemplate.exchange(
                url + "/user/login/" + loginName + "/" + loginPassWord,
                HttpMethod.GET, requestEntity, GuestTokenResp.class);
        return exchange.getBody();
    }
    /**
     * 更新或新增访客信息
     * https://segmentfault.com/a/1190000021123356
     */
    public GuestResp changeGusetInfo(GuestInfo guestInfo) {
        HttpHeaders headers = new HttpHeaders();
        System.out.println("token=="+getAccessToken().getData().getToken());
        headers.add(PARAM_TOKEN, getAccessToken().getData().getToken());
        headers.add(PARAM_SOURCE, VALUE_SOURCE);
        System.out.println(new Gson().toJson(guestInfo));
        HttpEntity<GuestInfo> requestEntity = new HttpEntity<>(guestInfo, headers);
        GuestResp exchange = restTemplate.postForObject(url + "/fk/change/fkxx", requestEntity, GuestResp.class);
        return exchange;
    }
    /**
     * 新增访客访问记录
     * @param guestRecord
     * @return
     */
    public GuestResp changeGusetRecord( GuestRecord guestRecord) {
        HttpHeaders headers = new HttpHeaders();
        System.out.println("token=="+getAccessToken().getData().getToken());
        headers.add(PARAM_TOKEN, getAccessToken().getData().getToken());
        headers.add(PARAM_SOURCE, VALUE_SOURCE);
        System.out.println(new Gson().toJson(guestRecord));
        HttpEntity<GuestRecord> requestEntity = new HttpEntity<>(guestRecord, headers);
        GuestResp exchange = restTemplate.postForObject(url + "/fk/insert/fkd/cljl", requestEntity, GuestResp.class);
        return exchange;
    }
}


controller测试


import io.wz.pojo.req.guest.GuestInfo;
import io.wz.pojo.req.guest.GuestRecord;
import io.wz.pojo.resp.guest.GuestResp;
import io.wz.pojo.resp.guest.GuestTokenResp;
import io.wz.service.GuestMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * api接口模拟调用
 */
@RequestMapping("/guestMachine")
@RestController
public class GusetMachineController {
    @Autowired
    private GuestMachineService guestMachineService;
    @GetMapping("/getToken")
    public GuestTokenResp getToken(){
        return guestMachineService.getAccessToken();
    }
    @PostMapping("/changeGusetInfo")
    public GuestResp changeGusetInfo(@RequestBody GuestInfo guestInfo){
        return guestMachineService.changeGusetInfo(guestInfo);
    }
    @PostMapping("/changeGusetRecord")
    public GuestResp changeGusetRecord(@RequestBody GuestRecord guestRecord){
        return guestMachineService.changeGusetRecord(guestRecord);
    }
}
相关文章
|
8月前
Copy网页中F12里的请求url到postman,并且把所有参数都带过来
Copy网页中F12里的请求url到postman,并且把所有参数都带过来
150 0
|
2月前
|
缓存 安全 数据安全/隐私保护
如何根据请求场景选择 GET 或 POST 请求方法?
【10月更文挑战第27天】根据不同的请求场景,综合考虑数据传输目的、安全性、数据量大小、幂等性要求以及缓存需求等因素,合理地选择GET或POST请求方法,能够更好地实现客户端与服务器之间的数据交互,提高系统的性能和安全性。
159 64
|
2月前
|
XML 缓存 安全
PUT 请求和 POST 请求有什么区别?
【10月更文挑战第25天】PUT请求和POST请求在HTTP协议中有着不同的功能和应用场景,开发者需要根据具体的业务需求和资源操作的性质来选择合适的请求方法,以确保客户端与服务器之间的交互准确、安全且符合预期。
Kam
|
Java Maven
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Kam
2231 0
|
3月前
|
JSON API 数据格式
postman如何发送json请求其中file字段是一个图片
postman如何发送json请求其中file字段是一个图片
189 4
|
7月前
|
安全 搜索推荐
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
|
8月前
|
XML JSON 前端开发
获取后端接口请求中的参数(@PathVariable,@RequestParam,@RequestBody区别,使用postman请求
获取后端接口请求中的参数(@PathVariable,@RequestParam,@RequestBody区别,使用postman请求
287 1
|
8月前
restTemplate 发送http post请求带有文件流、参数
restTemplate 发送http post请求带有文件流、参数
189 1
|
8月前
|
API
webclient在delete请求时携带request body
webclient在delete请求时携带request body
136 0
|
Java Spring
Java中restTemplate携带Header请求
Java中restTemplate携带Header请求
220 2