RestTemplate基本使用之HTTP实现GET请求和POST请求

一、GET请求实例

java 复制代码
    public static TianQi getTianQi(String city) {
        RestTemplate restTemplate = new RestTemplate();
        HashMap res = restTemplate.getForObject("http://www.tianqiapi.com/api/?version=v6&appid=15118158&appsecret=gVNnwva8&city=" + city, HashMap.class);

        if (res == null) {
            return null;
        }
        Object errCode = res.get("errcode");
        if (errCode != null) {
            log.error("获取天气错误,city:{},res:{}", city, JsonUtil.toJson(res));
            int code = (int) errCode;
            int error = 100;
            if (code == error) {
                return null;
            }
        }
        TianQi tianQi = new TianQi();
        tianQi.setCountry((String) res.get("country"));
        tianQi.setTem((String) res.get("tem"));
        tianQi.setTem1((String) res.get("tem1"));
        tianQi.setTem2((String) res.get("tem2"));
        tianQi.setCity((String) res.get("city"));
        tianQi.setWea((String) res.get("wea"));
        tianQi.setWeaImg((String) res.get("wea_img"));
        tianQi.setAirTips((String) res.get("air_tips"));
        tianQi.setDate((String) res.get("date"));
        tianQi.setUpdateTime((String) res.get("update_time"));
        return tianQi;
    }

二、POST请求实例

java 复制代码
package com.gzzoc.wisdom.personal.conv.business.helper;


import com.alibaba.fastjson.JSON;
import com.gzzoc.wisdom.common.model.personal.dto.ChatZOCDataDTO;
import com.gzzoc.wisdom.common.model.personal.dto.ChatZOCRequestDataDTO;
import com.gzzoc.wisdom.personal.conv.business.service.ConvDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * class: LLMChatZOCClient
 *
 * @author Selig
 * @date 2024-10-17 14:27
 */
@Slf4j
@Component
public class LLMChatZOCClient {

    private final static String URL = "url地址";

    public static ResponseEntity<String> getChatZOCData(ChatZOCRequestDataDTO dto) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        //默认流式返回
//        headers.setContentType(MediaType.TEXT_EVENT_STREAM);
        headers.add("Accept", "application/json");
        headers.add("Content-type", "application/json");
        ChatZOCDataDTO requestBody = new ChatZOCDataDTO();
        if (dto.getStream() != null) {
            requestBody.setStream(false);
        }
        requestBody.setMessages(dto.getMessages());
        log.info("requestBody----"+JSON.toJSONString(requestBody));

        //3、组装请求头和参数
        HttpEntity<String> formEntity = new HttpEntity<>(JSON.toJSONString(requestBody), headers);
        //4、发起post请求
        ResponseEntity<String> response = null;
        try {
            response = restTemplate.postForEntity(URL, formEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        //5、获取http状态码
        if (response != null){
            int statusCodeValue = response.getStatusCodeValue();
            log.info("httpCode-----"+statusCodeValue);
        }

        return response;
    }


}
相关推荐
小墙程序员1 分钟前
一文了解网络连接的完整流程
网络协议·tcp/ip
一个有女朋友的程序员9 分钟前
Spring Boot 整合 Smart-Doc:零注解生成 API 文档,告别 Swagger
java·spring boot·smart-doc
苹果醋330 分钟前
AI大模型竞赛升温:百度发布文心大模型4.5和X1
java·运维·spring boot·mysql·nginx
网安INF34 分钟前
CVE-2020-1938源码分析与漏洞复现(Tomcat 文件包含/读取)
java·网络·web安全·网络安全·tomcat·漏洞复现
nenchoumi31191 小时前
UE5 学习系列(九)光照系统介绍
java·学习·ue5
张乔241 小时前
spring boot项目整合mybatis实现多数据源的配置
java·spring boot·多数据源
GzlAndy1 小时前
Tomcat调优
java·tomcat
美好的事情能不能发生在我身上1 小时前
苍穹外卖Day11代码解析以及深入思考
java·spring boot·后端·spring·架构
辉辉健身中1 小时前
Maven入门(够用)
java·maven
星火飞码iFlyCode1 小时前
【无标题】
java·前端·人工智能·算法