【发布时间】:2023-03-26 20:25:01
【问题描述】:
我有 Spring Boot 应用程序,我在其中获取 streamName 作为参数,但现在我不希望它在邮递员中工作,而是在另一个程序中,其中 streamName 是调用函数时创建的字符串。以前我是给它作为json,但现在我想给它作为参数,我不知道我该怎么做。
这是我在 Spring Boot 中的请求:
@PostMapping
@ResponseBody
public String addStream(@RequestParam("streamName") String streamName) {
String key = getRandomHexString();
streamService.addStream(new Stream(streamName,key));
return key;
}
这是在另一个程序中,我想在其中制作此方法:
public void onHTTPPostRequest(String streamName) throws IOException {
PostResponse postResponse = new PostResponse();
postResponse.setStreamName(streamName);
Gson gson = new Gson();
String jsonString = gson.toJson(postResponse);
getLogger().info("POST Body " + jsonString);
URL pipedreamURL = new URL("http://10.100.2.44:8080/api?streamName=");
HttpURLConnection conn = (HttpURLConnection) pipedreamURL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json");
OutputStream os = conn.getOutputStream();
os.write(jsonString.getBytes("UTF-8"));
os.close();
int responseCode = conn.getResponseCode();
getLogger().info(responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
【问题讨论】:
标签: java spring spring-boot rest