简体   繁体   English

如何在angularjs中使用$ resource消耗Spring Restful Web服务

[英]how to consume spring restful web service using $resource in angularjs

I am developing a spring restful application with angularjs. 我正在用angularjs开发一个Spring Restful应用程序。 In Spring i am using maven and Hibernate. 在春季,我正在使用Maven和Hibernate。

I have returned json object using spring rest but i dont know how to use this object in my angular controller using $resource 我已经使用spring rest返回了json对象,但是我不知道如何使用$ resource在我的角度控制器中使用这个对象

Here is my spring rest controller 这是我的弹簧托控制器

@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody
List<Employee> getEmployee() {
List<Employee> employeeList = null;
    try {
employeeList = dataServices.getEntityList();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return employeeList;
}

Here is my jsp 这是我的jsp

<body>
<h3>FirstName:</h3>

<!-- First name from json object -->
<p></p>

<h3>LastName:</h3>

<!-- Last name from json object -->
<p></p>
</body>

So please help me with angularjs controller code 所以请帮助我使用angularjs控制器代码

My application name is: SpringRestCrud1 我的应用程序名称是:SpringRestCrud1

My path which is used to return the json object is: http://localhost:8080/SpringRestCrud1/employee/list 我用来返回json对象的路径是: http:// localhost:8080 / SpringRestCrud1 / employee / list

And my result is: [{"id":3,"firstName":"Hoston","lastName":"lindey","email":"hl@gmail.com","phone":"90908989899"}] 我的结果是:[{“ id”:3,“ firstName”:“ Hoston”,“ lastName”:“ lindey”,“ email”:“ hl@gmail.com”,“ phone”:“ 90908989899”}]

ngResource is very simple to use. ngResource使用起来非常简单。 How I typically use it is I first create a factory that is used to map to the CRUD endpoint: 我通常如何使用它,首先创建一个工厂用于映射到CRUD端点:

angular.module('angularRestCrud1')
  .factory('Employee', function ($resource) {
    return $resource('http://localhost:8080/SpringRestCrud1/employee/list', {}, {
      'query': {
        method: 'GET',
        params: { action: 'read', format: '.json' } , isArray : false
      }
    });
  });

From there you can set up a controller to access the list: 在这里,您可以设置控制器来访问列表:

angular.module('angularRestCrud1')
  .controller('EmployeeCtrl', function ($scope, Employee) {

    // Promise chain to resolve employee
    Employee.query(function (data) {
      $scope.employees = data;
    });

    });

And a view to show the employee list: 并显示员工列表的视图:

<body>
    <div ng-repeat="employee in employees track by $index">
        <h3>FirstName:</h3>
        <!-- First name from json object -->
        <p>{{ employee.firstName }}</p>

        <h3>LastName:</h3>
        <!-- Last name from json object -->
        <p>{{ employee.lastName }}</p>
    </div>  
</body>

I would suggest though that you redesign your endpoints to be more RESTful if you are going to use ngResource. 但我建议您如果要使用ngResource,则将端点重新设计为更加RESTful。 Here's a good guide for that. 这是一个很好的指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM