当前位置: 首页 > article >正文

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

一、GET请求实例

    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请求实例

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


}


http://www.kler.cn/news/359160.html

相关文章:

  • Pymysql中Mysql连接默认会开启事务处理-数据表名行数列表SQL以及python中的日志模板 logoru 及常用参数配置解析
  • 内网穿透之Linux系统安装神卓互联【超详细,小白一看就会】
  • 2024-moectf Web WP
  • docker配置加速器
  • git分支模型
  • 情绪稳定!别再让Git合并冲突影响你工作了
  • 音视频入门基础:H.264专题(19)——FFmpeg源码中,获取avcC封装的H.264码流中每个NALU的长度的实现
  • [论文阅读]Large Language Models Are Reasoning Teachers
  • 《深度学习》OpenCV 人脸检测、微笑检测 原理及案例解析
  • commvault测试(1):cv10使用辅助拷贝实现不同MA的数据同步
  • 在文件里引用目录文件下的静态资源图片不显示
  • Element笔记
  • MoCoOp_ Mixture of Prompt Learning for Vision Language Models
  • xlnt加载excel报错:xl/workbook.xml:2:2581: error: attribute ‘localSheetId‘ expected
  • Kaggle Python练习:字符串和字典(Exercise: Strings and Dictionaries)
  • PythonNet:实现Python与.Net代码相互调用!
  • 2024-10-14 问AI: [AI面试题] 机器学习中维度的诅咒是什么?
  • c语言基础程序——经典100道实例。
  • Could not retrieve mirrorlist http://mirrorlist.centos.org错误解决方法
  • 【原创】一键安装和更新ollama脚本