Good evening. I am trying to send the user object from the frontend to the spring controller. For some reason, the error 400 crashes (Bad Request). My service:

service.createUser = function (user) { console.log("Service. Creating user ",user); return http({ method: 'GET', url: '/user/createUser', contentType: 'application/json', data: {user: user} }) }; 

frontend user (filled with data. 1-2 roles):

 $scope.user = { firstName: "", lastName: "", email: "", password: "", roles: [] }; 

Spring Controller:

 @RestController @RequestMapping("/user") public class UserController { @RequestMapping(value = "/createUser", method = RequestMethod.GET) public boolean createUser(@RequestBody UserDTO userDTO) { System.out.println(userDTO); return true; }} 

Userdto

 public class UserDTO { private Long id; private String email; private String firstName; private String lastName; private String password; private List<Role> roles; //getters and setters } 

Please tell me how to fix it. Thank.

UPD:

Corrected the situation by adding .csrf (). Disable () to the spring security configuration. Now there is another error. The object for some reason comes empty (the fields are not initialized), although it is full before being sent to the console.

The object is full before being sent to the backend.

enter image description here

UPDT 2: The problem was that in the userdto field of roles there was a Role interface and not its implementation of RoleImpl.

    2 answers 2

    As far as I know, Angular cannot send a GET request with a message body. Moreover, the HTTP standard, although it does not prohibit the transfer of data in the body of GET requests, does not provide for the server to process it somehow.

    In your case you need:

    • or transfer data in query parameters:

       $http({ url: user.details_path, method: "GET", params: { id: user.id, ... } }); 

      but in this case, in the controller's method in Spring, you will need to use @RequestParam for each field instead of @RequestBody .

    • or, more correctly, use POST to create objects and PUT to modify.

      The object in this case comes to you empty, because in the request you have a User object inside the user field. Try to replace

       data: {user: user} 

      on

       data: user 
    • I did everything as you wrote (changed to the post method, and changed data: {user: user} to data: user). Now 400 Bad Request falls out. - Aleksei
    • It would be nice logs from the server. It's hard to guess. - Nofate
    • description of the client has been syntactically incorrect. - Aleksei
    • Check in the Developer Tools in the browser: is valid JSON sent? - Nofate
    • added a picture to the first post. The object is normally filled before sending - Aleksei

    While I can not leave comments, in your request url: '/ user / createUser', and in spring controller value = "/ createUser", should it be so? It is also supposed to use the PUT method for saving (update) an object.

     @RequestMapping(value = "/api/user", method = PUT) public ResponseEntity<User> updateUser(@RequestBody User user){ userService.saveUser(user); return new ResponseEntity<>(user, HttpStatus.OK); } 
    • As for the url, my entire class is marked as @RestController and @RequestMapping ("/ user"). Regarding data transfer methods, for some reason only get works for me. If, for example, change to post - pops up 403 (Forbidden) .. - Aleksei