Spring BootのREST APIとAngularJSの組み合わせ
最近、AngularJSも少し勉強しているのですが、「Spring Boot AngularJS」で検索したらサンプルが出てきたので、やってみました。
作り方
src/main/resources直下に「public」というフォルダを作り、そこに下記のようなJS・HTMLを作成します。
hello.js
function Hello($scope, $http) { $http.get('http://localhost:8080/api/customers'). success(function(data) { $scope.customers = data; }); }
home.html
<!doctype html> <html ng-app> <head> <title>Hello AngularJS</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <ul ng-repeat="customer in customers"> <li>{{customer.id}} {{customer.firstName}} {{customer.lastName}}</li> </ul> </div> </body> </html>
こんな感じです。
実行結果
作成できたら、メインメソッドを実行して起動し、ブラウザから下記のURLにアクセスします。
http://localhost:8080/home.html
こんな感じになったら成功です。
注意点
このサンプル、AngularJSの1.2系では動きますが、1.3系では動きません。
1.3系では何も表示されず、画面が真っ白になります。
この点は、引き続き調査します。
★追記
AngularJS 1.3系でのサンプルはコレあたりかな?
https://docs.angularjs.org/tutorial/step_11