S
S
Skoleev2021-08-15 16:28:32
Java
Skoleev, 2021-08-15 16:28:32

What is the best way to pass a single parameter to a JSON Post?

Should I create a POJO with a single field and get it via @RequestBody or is it better to directly specify @RequestBody with Integer?

Like this for example

@PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@AuthenticationPrincipal AuthenticatedUser authenticatedUser,
                       @RequestBody Integer clientId) {
        clientService.add(authenticatedUser.getUser(), clientId);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2021-08-15
@Skoleev

If in the future it may be necessary to expand the set of transmitted fields, then using now POJO with one field, then you will have to rewrite less.

E
EVGENY T., 2021-08-17
@Beshere

The real practice in production is to have dto-classes under web controllers that are passed in the request parameter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question