对Spring端点的Java请求



我正试图从Java应用程序请求另一个应用程序是在春天写的。现在我得到一个400

这是我试图到达的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,
@RequestParam(value = "Text", required = true) String text
) throws Exception

,这是我尝试发出一个有效的请求:

private void getTtsWave(String waveId, String token, int file_id, String tts_text) {        
try {
URL url = new URL(this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "/{text}?Text=" + tts_text);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Bearer " + token);
int status = con.getResponseCode();
System.out.println(status);
if (status == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}

if (content != null && !content.equals("")) {
System.out.println(content);
}

in.close();
}
} catch (Exception e) {
log("getTtsWave error: " + e, e.toString()); 
}
}
@GetMapping(value="/tts/{sessionid}/{fileId}/{text}")
ResponseEntity<byte[]> getAudioFile

期望{text}@PathVariable,但在getTtsWave方法中,您将其视为"静态";url的一部分:

this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "/{text}?Text=" + tts_text

此外,在你的getAudioFile中,你也有一个参数:

@RequestParam(value = "Text", required = true) String text

这个参数是必需的请求参数(notpath)param)

所以我认为你应该改成:

@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,
@RequestParam(value = "Text", required = true) String text
) throws Exception

并将URL构造为:

this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "?Text=" + tts_text