Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestBody not working on Rest service

I am developing a web application with AngularJS and WildFly using Spring also.

My problem is that I am going nuts because the annotation @requestBody appears to work wrong.

This is my service:

@ResponseBody
@RequestMapping(value = "/keyuser", method = RequestMethod.POST,
  consumes = "application/json")
public KeyProfileUserSummary updateEmployee(@RequestBody KeyProfileUserSummary keyUser) {
return null;
}

And this is are the members of my object KeyProfileUserSummary:

private Integer id;
private String login;
private String password;
private String firstname;
private String lastname;
private UserRole userRole;

I don't know what is going on but I have tested this service with other types of objects and it works perfectly, but when defining KeyProfileUserSummary it is not working, I get ERROR 400 BAD REQUEST. I have tested to set the @RequestBody to "Object" so at least I can see what is coming, and from my front end, I am getting the following:

{id=3, login=aa, password=a, firstname=Martin, lastname=Müller, userRole=ROLE_USER} 

UserRole is an Enum. Important to clearify that KeyProfileUserSummary is just a summary version of KeyProfileUser, but due to all the linked elements I get on the response, I decided to send this lighter class. Testing with KeyProfileUser worked perfectly, I get the JSON object on the Angular side and can send it back.

On the Angular side, I am not doing anything with the object. Just receive it on a list, and when pressing an edit button just send the element on the list back. This is the way I am sending it:

res = $http.post("url.../keyuser", user);

The thing is that I had everything working perfectly with KeyProfileUser, but as the database can get really huge and the reference are quite a lot, I decided to switch to this lighter class, but now I only get this ERROR 400 BAD REQUEST... And I am about to hang myself :P

Thanks for your help!

like image 874
Carloshf Avatar asked Mar 03 '16 17:03

Carloshf


2 Answers

Ok so finally I found the solution.

In my KeyProfileUserSummary I only had one constructor that was taking a KeyProfileUser and set the attributes to the summary version:

public KeyProfileUserSummary(KeyProfileUser keyProfileUser) {
  this.id = keyProfileUser.getId();
  this.login = keyProfileUser.getLogin();
  this.password = keyProfileUser.getPassword();
  this.firstname = keyProfileUser.getPerson().getFirstname();
  this.lastname = keyProfileUser.getPerson().getLastname();
  this.userRole = keyProfileUser.getUserRole();
}

And apparently, setting a breakpoint in line 993 of the dispatchler servlet (thanks to @Clemens Eberwein for the tip) I realised that when parsing from a JSON object, the Jackson parser needs an empty constructor! So adding it solved it and works perfectly.

Note: for KeyProfileUser, it was working perfectly as we had the annotation @Entity for hibernate, and therefore the empty constructor was automatically created.

like image 143
Carloshf Avatar answered Sep 25 '22 07:09

Carloshf


Try this out.. might be useful for you..

$http({
    method: 'POST',
    url: 'http://localhost:8080/keyuser',
    data: user,
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }}).then(function(result) {
           console.log(result);
       }, function(error) {
           console.log(error);
       });
like image 25
Vikrant Kashyap Avatar answered Sep 24 '22 07:09

Vikrant Kashyap