0

我想获取 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);

}

结果邮递员: 在此处输入图像描述

日志:2021-07-30 13:15:30.376 WARN 4398 --- [nio-8080-exec-8] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法构造com.example.restservice.PostResponse(尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法可以从字符串值('aes')反序列化;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造实例com.example.restservice.PostResponse(尽管至少存在一个 Creator):没有字符串参数构造函数/工厂方法可从 [来源: (PushbackInputStream); 行:1,列:1]]

4

1 回答 1

0

您收到“aes”

那不是有效的 JSON。

您的端点接受的 JSON 必须是:

{
  "streamName": "aes"
}

它可以反序列化为 PostResponse 。

在客户端中,您可以使用此类:

PostResponse postResponse = new PostResponse();
postRepsone.setStreamName(streamName);
Gson gson = new Gson();
String jsonString = gson.toJson(postResponse);
于 2021-07-30T11:35:00.380 回答