0

I have the following code in my angularjs

$http({
                method : "POST",
                url : '/jobseeker',
                data : {
                    "email" : $scope.userdata.uemail,
                    "firstname" : $scope.userdata.fname,
                    "lastname" : $scope.userdata.lname,
                    "password" : $scope.userdata.upassword
                }
            }).success(function(data) {
                console.log(data);
                //state.go(to verification page for job seeker
            })

I have the following request mapping

@RequestMapping(value = "/jobseeker", method = RequestMethod.POST)
    public ResponseEntity signUp(@RequestBody Map<String, Object> parameterMap ) {...}

to accept this data.

Is there any better way to do this? Are there any request message converters which I can include in my pom.xml and simply use @RequestParam annotation to access the keys in the json data?

Note: I am new to spring boot and spring mvc and trying to learn

Neeraj
  • 58
  • 1
  • 3
  • 9
  • what is the problem? Did it work? – Raghu Venmarathoor May 10 '17 at 05:43
  • yes it works but is there any better way? – Neeraj May 10 '17 at 05:44
  • what are you trying to achieve? You are using `@RestController`, right? – Raghu Venmarathoor May 10 '17 at 05:45
  • yes I am trying to send key value pairs to my rest controller mapping. I am accepting this in the map object. Is there any better way to pass post request data to spring rest controller. – Neeraj May 10 '17 at 05:49
  • There will be 'other ways' to do the same thing, but why? This doesn't look like a real question. People might downvote this. I've written a lot of REST services and this is how I did it in most of the cases. Have a look in http://stackoverflow.com/a/26848992/796400 . May be, you will need to see other ways of doing it. – Raghu Venmarathoor May 10 '17 at 05:57
  • If you have a class such as `class User { String email; String firstname; String lastname; String password; }`, you can simply have Spring MVC map the request parameters to class attributes as `signUp(@RequestBody User user)`. The controller method will get a fully populated `User` object to work. This will of course require a JSON parser such as Jackson or GSON to be on the runtime classpath. – manish May 10 '17 at 06:05

1 Answers1

0

Spring boot implictly uses a json deserialization library (jackson) to map json input to java objects.

  1. Define a pojo that holds the fields you want to receive - e.g. SignupDto

  2. Define the following method in your Controller

@RequestMapping(path="/jobseeker", method=RequestMethod.POST)
    public Response update(@RequestBody SignupDto input, @Context HttpServletResponse response) {

Now you can work with the instance of SignupDto with your mapped values.