2

I created code where pass data to spring rest but don't work correctly see my code below:

@PutMapping("/notification/save/{id}")
public @ResponseBody String update(@PathVariable("id") long id, @RequestBody  AccountNotification accountNotification){
    String result="ok";
    ServiceAccount serviceAccount =  new ServiceAccount();
    serviceAccount.setId(id);
    //accountNotification.setServiceAccount( serviceAccount );
    //result =notificationsService.update(id,accountNotification);

    JSONObject jObject = new JSONObject();
    try
    {
        JSONArray jArray = new JSONArray();
             JSONObject accountNotificationJSON = new JSONObject();

             if(result.equals("ok")) {
                 accountNotificationJSON.put("success", "true");
                 accountNotificationJSON.put("messages", "Successfully Saved");
             }
             else {
                 accountNotificationJSON.put("success", "false");
                 accountNotificationJSON.put("messages", "NOT Successfully Saved");

             }
             jArray.put(accountNotificationJSON);

        jObject.put("data", jArray);
    } catch (JSONException jse) {
        logger.error( jse.getMessage());
    }


    return jObject.toString();
}

my javascrit has:

$("#saveChanges").on('click',function(){
     var params1={
             proxy:$(proxy).val() ,
             port:$(port).val(),
             uri:$(uri).val(),
             actived:$(actived).val()=="on"?true:false
     };

     $.ajax({
         url: 'notification/save/'+id,
         params: params1,
         type: 'post',
         success:function(response) {
             if(response.data[0].success == "true") {                      
                 $(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.data[0].messages+
                     '</div>');


                 // close the modal
                 $("#editMember").modal('hide');

             } else {
                 $(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.data[0].messages+
                     '</div>');
             }
         },
         error: function(x, e) {
                alert(x);
              }

     });

in this situation ajax code return 400. Any body knows why? thanks if withdraw @RequestBody annotation the rest has been called but the parameter accountNotifications is initialized but without valeus passed.

2
  • Change PutMapping toRequestMapping Commented Nov 23, 2017 at 11:47
  • dont work to, I had tryed before this mode : @RequestMapping(value={"/notification/save/{id}"},produces = "application/json", method= {RequestMethod.POST}) Commented Nov 23, 2017 at 11:54

2 Answers 2

1

there is a put mapping @PutMapping("/notification/save/{id}") in your Rest code, but in your js called by post method type: 'post', so it returns 400 Bad Request, you should use type: put which is equals to your Rest method.

 @PutMapping("/notification/save/{id}")
 public @ResponseBody String update(@PathVariable("id") long id, 
 @RequestParam  AccountNotification accountNotification){
 .....
 }



  $.ajax({
     url: 'notification/save/'+id,
     params: params1,
     type: 'put',
     success:function(){
 }})
Sign up to request clarification or add additional context in comments.

5 Comments

{"timestamp":1511437558654,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public java.lang.String br.com.supportcomm.trust.web.NotificationController.update(long,br.com.supportcomm.trust.model.AccountNotification)","path":"/next-trust/notification/save/4"}
can you able to post your AccountNotification class
you don't send a body (data parameter in JQuery)
There is a mismatch between your entity class parameters and data which you are sending through ajax request.
@Simon Martinelli mentioned correctly you should use @RequestParam annotations
0

I resolved this mode:

@RequestMapping(value={"/notification/save/{id}"},  method = {RequestMethod.GET, RequestMethod.POST},
         produces = "application/json"

        )
public @ResponseBody String update(@PathVariable("id") long id, AccountNotification accountNotification){
    String result="ok";

JavaScript:

 $("#saveChanges").on('click',function(){
		 var params1={
				 proxy:$(proxy).val() ,
				 port:$(port).val(),
				 uri:$(uri).val(),
				 actived:$(actived).is(':checked')
		 };
		 
         $.ajax({
             url: 'notification/save/'+id,
             data: params1,
             type: 'post',
             success:function(response) {

I replaced into js param with data

Comments

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.