Unable to get same response data back from POST request in java

Viewed 26

I have been working with Spring MVC controllers and now I landed on a problem. So basically what I want to do is if I send a POST request with whatever the input is, I should get the exact same response what the input was.

I have just this one controller and tried to do the following:

import lombok.Data;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RestController
public class Controller {

    @Data
    public class DataField {
        public String content;
    }

    @GetMapping("/echo")
    public void sayHello() {
    }

    @PostMapping("/echo")
    public String postHello(@RequestBody DataField data) {
        System.out.println("post: " + data);
        DataField content = data;
        return content.content;
}

If I post something i.e

String inputJson = """
                {"a":1,"b":2,"c":3}""";

how am I able to get the same response input back from the same address?

Current error:

Expected: "{\"a\":1,\"b\":2,\"c\":3}"
     but: was ""
0 Answers
Related