Receiving object by parameter

1

I'm using Spring MVC and I use some mappings to receive an object for example and access that specific object in controller .

For example:

@GetMapping("/pessoa/{idPessoa}")
public ModelAndView editaPessoa(@PathVariable("idPessoa") Pessoa pessoa) {
.....
}

So if I have a listing, I need to go through a link in the controller's href, passing the id of the person I wanted to edit, for example. However, I wanted to hide the id of the person via the browser, so that the url was not exposing the ids of the objects

link

Is there any way to just call the mapping without passing the id by url and being able to retrieve that object inside the controller?

    
asked by anonymous 13.12.2018 / 20:06

1 answer

0

Yes, just use the @RequestBody and create a POJO class that contains an attribute of String id. Note that you will not be able to pass the href of the link, so you will have to create a request by forwarding json via Javascript.

For example:

@GetMapping("/pessoa/")
public ModelAndView editaPessoa(@RequestBody PessoaInterface pessoa) {
.....
}

PersonaInterface.java

public class PessoaInterface {
    String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Note that in PersonInterface.java you can add a List and pass through numerous IDs in a single request.

Ideal solution

You are using the GET method to do an edit. One of the principles of REST is to respect HTTPs codes and use the correct verbs, that is, to use GET only in a case where you need to access something . Ideally you should use PUT or PATCH for editing (I can not say which one I'm sure because I do not know your scenario).

Note that it is not only a matter of respecting the standards, there is a long thread about forwarding body in a GET request ( link ) and some servers even refuse by default requests GETs with body. Speaking of REST, this is certainly not supported.

What if you wanted to get an element without having to go through the ID?

Assuming that your problem is not a person edit but a person passing the ID, in these cases we continue to go through path param but GUIDs are used instead of IDs.

    
20.12.2018 / 22:11