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

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.lryc.cn/news/463338.html

相关文章:

  • 2024-10-18 问AI: [AI面试题] 神经网络有哪些不同类型?
  • 【开源免费】基于SpringBoot+Vue.JS课程作业管理系统(JAVA毕业设计)
  • jmeter中对于有中文内容的csv文件怎么保存
  • Leetcode 921 Shortest Path in Binary Matrix
  • 第二十二篇——菲欧几何:相对论的数学基础是什么?
  • 【AI整合包及教程】EchoMimic:开创数字人新时代,让静态图像“活”起来!
  • ArcGIS 最新底图服务地址
  • 【服务器部署】Docker部署小程序
  • 三菱FX PLC设计一个电子钟程序实例
  • 妇女、商业与法律(WBL)(1971-2023年)
  • python 卸载、安装、virtualenv
  • ubuntu24.0离线安装Ollama和纯cpu版本以及对接Spring AI
  • 机器学习核心:监督学习与无监督学习
  • 服务器托管的优缺点有哪些?
  • RestClient查询文档排序、分页和高亮
  • API项目5:申请签名 在线调用接口
  • Google FabricDiffusion:开启3D虚拟试穿新篇章
  • 【开发语言】c++的发展前景
  • 【机器学习】图像识别——计算机视觉在工业自动化中的应用
  • lstm基础知识
  • Linux :at crontab简述
  • Python,Swift,Haskell三种语言在使用正则表达式上的方法对比
  • leetcode力扣刷题系列——【三角形的最大高度】
  • 工业相机解决方案
  • 设计一个高效的日志分析系统:自动检测错误日志的实用指南
  • 英语学习--如果你的父母不听你的话
  • LeetCode:3258.统计满足k约束的子串数量 I(滑动窗口 Java)
  • 如果用Java设计MySQL中表级锁、行级锁和间歇锁会是怎么的?
  • GIT batch的支持中文的方法和系统建议
  • 骑砍霸主MOD天芒传奇Ⅱ·前传-序章