You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java应用调用Spring GET接口返回400错误问题排查

问题原因
  • 路径参数未填充:Spring端定义的接口路径为/tts/{sessionid}/{fileId}/{text},包含3个占位符路径参数,你构造请求URL时第三个路径段直接写死为{text}字符串,没有替换为实际值,导致路径无法匹配服务端路由规则。
  • 参数定义不匹配:Spring端路径中声明了第三个路径变量{text},但方法参数中没有对应的@PathVariable("text")注解接收该参数,反而额外声明了必填的查询参数Text,参数规则存在冲突。
  • 特殊字符未编码:你直接将tts_text拼接至URL中,如果文本包含空格、中文、特殊符号等内容,未做URL编码会导致请求格式非法。
修复方案

首先明确服务端接口的设计逻辑,选择对应方案:

方案1:保留查询参数逻辑(推荐,长文本不适合放路径中)

  1. 先修改Spring端接口定义,删除路径中多余的{text}占位符:
// 修正后的Spring接口
@GetMapping(value="/tts/{sessionid}/{fileId}")
ResponseEntity<byte[]> getAudioFile(
        @ApiParam(value = "Wave SessionId", required = true) @PathVariable String sessionid,
        @ApiParam(value = "File id", required = true) @PathVariable Integer fileId,
        @ApiParam(value = "TTS文本内容", required = true) @RequestParam(value = "Text", required = true) String text
) throws Exception
  1. 修改请求端URL构造逻辑,删除多余路径段,同时对查询参数做URL编码:
// 修正后的请求代码
private void getTtsWave(String waveId, String token, int file_id, String tts_text) {        
    try {
        String encodedText = URLEncoder.encode(tts_text, StandardCharsets.UTF_8.name());
        URL url = new URL(this.recorderendpoint + "/api/tts/" + waveId + "/" + file_id + "?Text=" + encodedText);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Authorization", "Bearer " + token);
        // 后续原有逻辑保持不变

方案2:保留路径参数逻辑

  1. 修改Spring端接口定义,补充路径参数接收注解,删除多余的查询参数声明:
// 修正后的Spring接口
@GetMapping(value="/tts/{sessionid}/{fileId}/{text}")
ResponseEntity<byte[]> getAudioFile(
        @ApiParam(value = "Wave SessionId", required = true) @PathVariable String sessionid,
        @ApiParam(value = "File id", required = true) @PathVariable Integer fileId,
        @ApiParam(value = "TTS文本内容", required = true) @PathVariable String text
) throws Exception
  1. 修改请求端URL构造逻辑,填充路径参数并编码,删除查询参数部分:
// 修正后的请求代码
private void getTtsWave(String waveId, String token, int file_id, String tts_text) {        
    try {
        String encodedText = URLEncoder.encode(tts_text, StandardCharsets.UTF_8.name());
        URL url = new URL(this.recorderendpoint + "/api/tts/" + waveId + "/" + file_id + "/" + encodedText);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Authorization", "Bearer " + token);
        // 后续原有逻辑保持不变

额外校验:如果Spring端控制器类上没有添加@RequestMapping("/api")注解,请求URL中的/api前缀也需要同步删除,否则会出现路径不匹配问题。

内容的提问来源于stack exchange,提问作者k-wasilewski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 20:09:03