欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-07 15:40:51
...

文章目录

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

}

相关标签: spring家族