【发布时间】:2021-10-05 22:42:24
【问题描述】:
我想获取 streamName 参数,该参数是在我发出 HTTPRequest 的程序中执行一种方法时生成的,作为响应,我想获取生成的十六进制字符串。我找不到丢失的东西。
这是我在 Spring Boot 中的代码:
@PostMapping(value="/test", consumes = "application/json", produces = "application/json")
public String Test(@RequestBody PostResponse inputPayload) {
PostResponse response = new PostResponse();
response.setStreamName(inputPayload.getStreamName());
String randomHexString = getRandomHexString(32);
return randomHexString;
}
PostResponse 类:
public class PostResponse {
String streamName;
public String getStreamName() {
return streamName;
}
public void setStreamName(String streamName) {
this.streamName = streamName;
}
}
我提出请求的另一个程序上的方法:
public void onHTTPPostRequest(String streamName) throws IOException {
String post_data = streamName;
Gson gson = new Gson();
String jsonString = gson.toJson(post_data);
jsonString.toString();
URL pipedreamURL = new URL("http://10.100.2.44:8080/test");
HttpURLConnection conn = (HttpURLConnection) pipedreamURL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
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();
getLogger().info(response.toString());
Gson gson1 = new Gson();
keyjson = gson1.toJson(response);
}
邮递员的结果: enter image description here
日志: 2021-07-30 13:15:30.376 WARN 4398 --- [nio-8080-exec-8] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法构造 @987654325 的实例@(尽管至少存在一个 Creator):没有 String-argument 构造函数/工厂方法可以从 String 值反序列化('aes');嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造
com.example.restservice.PostResponse的实例(尽管至少存在一个 Creator):没有从字符串值反序列化的字符串参数构造函数/工厂方法('aes') 在 [来源:(PushbackInputStream);行:1,列:1]]
【问题讨论】:
-
您发送的 JSON 看起来如何?
-
jsonString 是“aes”
标签: java spring spring-boot rest