Spring REST - Deserializing object from GET request query

556 Views Asked by At

I'm trying to implement an endpoint that takes a serialized object from request parameter and deserializes it into a POJO. Is there an easy way how to do this with Spring?

The example of a query: http://localhost/routes/departures?trip=%7B%22stopId%22:%22U321Z102%22,%22routeId%22:%22L991D1%22,%22headSign%22:%22Nemocnice+Motol%22%7D which should translate into this:

trip: {"stopId":"U321Z102","routeId":"L991D1","headSign":"Nemocnice Motol"}

Also, those parameter values may contain spaces and special characters (ěščř...). Will Spring handle this? Alternatively I could send those parameters separately and not serialized, but I'm worried this would be an issue.

1

There are 1 best solutions below

1
MC Ninjava On

You need to send the user by using post request (send a userDTO that has the same type and attributes names than the one in the back end )

your rest controller is going to look like this

    @PostMapping("/users")
    @PreAuthorize("hasRole(\"" + AuthoritiesConstants.ADMIN + "\")")
    public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO)      throws URISyntaxException {
    log.debug("REST request to save User : {}", userDTO);