Skip to content
Advertisement

spring boot REST POST method doesn’t work

I want to get the streamName parameter, which is generated when executing one method in the program where I made HTTPRequest and in response I want to get the generated hex string. I couldn’t find what is missing.

This is my code in 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 class:

public class PostResponse {
    String streamName;

    public String getStreamName() {
        return streamName;
    }

    public void setStreamName(String streamName) {
        this.streamName = streamName;
    }

}

method on another program where i make a request:

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);

}

results in postman: enter image description here

Log: 2021-07-30 13:15:30.376 WARN 4398 — [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.example.restservice.PostResponse (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘aes’); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.example.restservice.PostResponse (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘aes’) at [Source: (PushbackInputStream); line: 1, column: 1]]

Advertisement

Answer

You are receiving “aes”

That’s not valid JSON.

The JSON your endpoint is accepting must be:

{
  "streamName": "aes"
}

That it can be deserialized to PostResponse .

In the client you can use this class to:

PostResponse postResponse = new PostResponse();
postRepsone.setStreamName(streamName);
Gson gson = new Gson();
String jsonString = gson.toJson(postResponse);
Advertisement