response.getStatusCode() - 页面请求状态码


页面请求的状态值,分别有:200请求成功、303重定向、400请求错误、401未授权、403禁止访问、404文件未找到、500服务器错误

2025-09-05 15:28:15.244 [traceId-] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter:40 - *************************** APPLICATION FAILED TO START *************************** Description: Parameter 1 of constructor in com.altair.userserver.controller.AIDify.AssistantController required a bean of type 'org.springframework.http.HttpHeaders' that could not be found. Action: Consider defining a bean of type 'org.springframework.http.HttpHeaders' in your configuration.错误信息 package com.altair.userserver.controller.AIDify; import com.altair.userserver.entity.DifyRequest; import com.altair.userserver.entity.DifyResponse; import lombok.extern.slf4j.Slf4j; 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.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/api/assistant") @Slf4j public class AssistantController { private final RestTemplate restTemplate; private final HttpHeaders headers; private final String difyUrl; @Autowired public AssistantController(RestTemplate restTemplate, HttpHeaders headers, @Value("${dify.api.url}") String difyUrl) { this.restTemplate = restTemplate; this.headers = headers; // 关键:确保请求头“Accept”与对方响应格式匹配(对方返回HTML,就接受HTML) this.headers.set("Accept", "text/html, application/xhtml+xml, */*"); this.difyUrl = difyUrl; } @PostMapping("/chat") // 响应类型改为 String(直接返回HTML字符串),无需抛JsonProcessingException public ResponseEntity<String> chat(@RequestBody DifyRequest request) { try { // 1. 构建请求(携带修改后的请求头) HttpEntity<DifyRequest> entity = new HttpEntity<>(request, headers); log.info("调用Dify接口请求:URL={}, 请求体={}, 请求头={}", difyUrl, request, headers); // 2. 调用接口(直接接收String类型响应,不提前解析) ResponseEntity<String> response = restTemplate.postForEntity( difyUrl, entity, String.class ); // 3. 处理响应:分状态码判断 if (response.getStatusCode().is2xxSuccessful()) { String htmlResponse = response.getBody(); log.info("Dify接口返回HTML成功,长度={}", htmlResponse != null ? htmlResponse.length() : 0); // 关键:返回HTML时,设置响应头Content-Type为text/html,避免前端解析错乱 HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("Content-Type", "text/html; charset=utf-8"); return new ResponseEntity<>(htmlResponse, responseHeaders, HttpStatus.OK); } else { // 非2xx状态(如404、500):记录错误并返回状态码 String errorMsg = "Dify接口调用失败,状态码=" + response.getStatusCode() + ",响应内容=" + response.getBody(); log.error(errorMsg); return ResponseEntity.status(response.getStatusCode()).body(errorMsg); } } catch (RestClientException e) { // 捕获HTTP调用异常(如连接超时、URL不可达) String errorMsg = "调用Dify接口时发生网络异常"; log.error(errorMsg, e); // 打印完整堆栈,便于排查 return ResponseEntity.status(HttpStatus.BAD_GATEWAY) .body(errorMsg + ":" + e.getMessage()); } } }控制层内容
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值