0

Hi everyone i'm trying to send data as param to spring mvc method that should catch param using @RequestParam :

@ResourceMapping(value="send")  
public void send(ResourceResponse response,@RequestParam("message") String message) throws JsonGenerationException, JsonMappingException, IOException{      
    System.out.println("send method invocked");
    System.out.println("message === >" + message);
    .........

and my angular JS script (not work) is as follow

 var message = "message="+JSON.stringify({
                "name" : $scope.message.name ,
                "email" : $scope.message.email ,
                "tel": $scope.message.tel,
                "id_subject":$scope.message.selectedSubject ,
                "content" : $scope.message.content
              });
              console.log("valid");
              $http.post('${send}', message)
              .success(function(data, status, headers, config) {

              })
              .error(function(data, status, headers, config) {

              });

method from controller throw exception (Required String parameter 'message' is not present) please help

2
  • You should try @RequestBody with POST httpmethod, it is better approach to handle json data Commented May 21, 2015 at 17:59
  • it s JSR 286 not a simple spring web mvc Commented May 21, 2015 at 20:18

1 Answer 1

2

Controller.java:

@RequestMapping(value = "/send", 
method = {RequestMethod.POST}, 
consumes = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public void place(@RequestBody Message msg) {
    //do something with msg
}

Message.java:

public class Message {

    //All your fields
    private String name;
    private String email
    //and so on...

    /*
    * Getters and setters for the fields.
    * You can use @Data annotation from Lombok library 
    * to generate them automatically for you.
    */
    public String getName() { return name; }
    public String getEmail() { return email; }
}

Angular part:

var message = {name: $scope.message.name, email: $scope.message.email};
$http.post('/send', message)
  .success(function() {
    console.log("msg sent");
  })
  .error(function() {
    console.log("msg failed");
  });

You may also need to configure Spring to use Jackson for JSON conversion:

Is it possible to convert from JSON to domain object with @RequestParam

Sign up to request clarification or add additional context in comments.

1 Comment

first thank you for repley Mr Arseny. your code is for spring web MVC application i use JSR 286 ( portlet api 2 ) with Spring web MVC portlet so i can't use @RequestMapping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.