0

I want to define a large json response body as @RequestBody parameter. Additionally I'd like to directly get some of the body parameters as

@RestController
public class MyController {
    @PostMapping("/")
    public Rsp test(@RequestBody JsonDTO json) {
    }
}

public class JsonDTO {
    private String json; //should contain the complete json object

    //additionally want to map some specific properties explicit
    private String test;
}

I want to send the following json content:

{
    "somecontent": "asd",
    ...
    "test": "imtest"
}

Then I need both the complete json string, and a DTO that maps only some of the parameters.

Is that possible?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • You can take the whole json as String and then convert it to whichever Object you want using ObjectMapper – pvpkiran Feb 06 '18 at 12:52
  • You mean manually converting using `readValue()`? That would be an option, though I would prefer `spring` to inject the parsed DTO, as then also handlers like BeanValidators are applied automatically. – membersound Feb 06 '18 at 12:58
  • 2
    I don't think you can do this. Jackson ObjectMapper uses HttpServletRequest inputStream to read data and convert to DTO. Once this is done, Stream would end, which you cannot read it again. You can convert json to DTO and vice versa yourself any time – pvpkiran Feb 06 '18 at 13:05

0 Answers0