1

I have a html/angular view which needs to get data from spring mvc controller which returns Json response.

Previously I used angular, getting json calling a REST url.

Not sure how to do the same when spring mvc controller returns a json response.

Thanks.

Rk R Bairi
  • 1,289
  • 7
  • 15
  • 39

2 Answers2

2

My sample code.

js

function sendMessage(message) {

    $.ajax({
        url : "/sample/push/" + message,
        processData : false,
        contentType : "text/html; charset=utf-8",
        type : 'POST',

        success : function(response) {
             // get response  
        },

        error : function(request, status, error) {

        },

    });
}`

controller

@RequestMapping(value = "/push/{message}")
public @ResponseBody String processResult(@PathVariable String message) {
    // "your json String"
    return pushService.pushMessage(message);
}

ajax call and spring MVC tutorial - link : this tutorial XD

0gam
  • 1,343
  • 1
  • 9
  • 21
  • I disagree with this answer though it is a correct way to do it (but out of context), since this uses _jQuery_ and the asker's question states that _"I have a html/angular view"_ and this is not angular at all. Maybe the asker doesn't care about whether it's angular or not, in that case this could be fine. If so, he should change the tag **angular** to **jquery** – lealceldeiro Feb 24 '17 at 14:50
1

Keep in mind some concepts as partial view, angular controller, angular service and how to make async call using the $http angular service.

Basically you create a Controller (js), a Service (js) and a partial View (html)

  • In the service you implement all data logic and rest api calls
  • In the controller you manipulate data retrieved using the service and prepare it to be presented in the partials
  • In the partial you "bind" (and it is shown to the user all data, actions, etc) the info in the controller
Graham
  • 7,431
  • 18
  • 59
  • 84
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80