【问题标题】:Consuming Java REST service with AngularJS (issue with UPDATE, DELETE)使用 AngularJS 使用 Java REST 服务(更新、删除问题)
【发布时间】:2017-04-11 10:11:41
【问题描述】:

我已经用 Java 实现了一个 REST 服务,当我用 Postman 测试它时,所有的 HTTP 方法都能正常工作。现在我决定更多地了解 AngularJS 并添加它以使用 REST 服务。 GET 请求工作正常,所有产品都显示在 html 页面上。但由于某种原因,Delete 和 Put 方法根本不起作用。而且我很难弄清楚是什么导致了这种行为。

我注意到问题出现在涉及产品 ID 的方法上。实体 Product.java 有一个名为 prod_id.

的 id 字段

app.js

angular.module("AppProducts", [])
.constant("baseUrl", "http://localhost:8080/webstore/product")
.controller("ProductsCtrl", function ($scope, $http, baseUrl) {

    $scope.currentView = "table";

    //Works correctly
    $scope.showAll = function () {
        $http.get(baseUrl).success(function (data) {
            $scope.products = data;
        });
    }
    //if product exists, copy it, otherwise new empty
    $scope.editOrCreate = function (product) {
        $scope.currentProduct = product ? angular.copy(product) : {};
        $scope.currentView = "edit";
    }

    $scope.create = function (product) {
        $http.post(baseUrl, product).success(function (product) {
            $scope.products.push(product);
            $scope.currentView = "table";
        });
    }

    $scope.update = function (product) {
        $http({
            url: baseUrl + product.prod_id,
            method: "PUT",
            data: product
        }).success(function (modifiedItem) {
            for (var i = 0; i < $scope.products.length; i++) {
                if ($scope.products[i].prod_id == modifiedItem.prod_id) {
                    $scope.products[i] = modifiedItem;
                    break;
                }
            }
            $scope.currentView = "table";
        });
    }

    $scope.delete = function (product) {
        // HTTP DELETE
        $http({
            method: "DELETE",
            url: baseUrl + product.prod_id
        }).success(function () {
            $scope.products.splice($scope.products.indexOf(product), 1);
        });
    }

    // Save changes
    $scope.saveEdit = function (product) {
        if (angular.isDefined(product.prod_id)) {
            $scope.update(product);
        } else {
            $scope.create(product);
        }
    }

    $scope.cancelEdit = function () {
        $scope.currentProduct = {};
        $scope.currentView = "table";
    }

    $scope.sortType     = 'brand'; // set the default sort type
    $scope.sortReverse  = false;  // set the default sort order
    $scope.searchProduct   = '';     // set the default search/filter term

    $scope.showAll();

});

“表格”视图

            <table id="myTable" class="table table-hover">
                <thead>
                <tr>
                    <th>Brand</th>
                    <th>Product Name</th>
                    <th>Description</th>
                    <th>Price</th>
                    <th width="100"></th>
                    <th width="100"></th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="product in products | orderBy:sortType:sortReverse">
                    <td>{{product.brand}}</td>
                    <td>{{product.productName}}</td>
                    <td>{{product.description}}</td>
                    <td>{{product.price}}</td>

                    <td><button class="btn btn-success" ng-click="editOrCreate(product)">Edit</button></td>
                    <td><button class="btn btn-danger" ng-click="delete(product)">Delete</button></td>
                </tr>
                </tbody>
            </table>

RestController '删除'方法

@RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteProduct(@PathVariable("id") int id) {

        Product product = productService.getProductById(id);
        if (product == null) {
            return new ResponseEntity(new CustomError("Unable to delete. Product with id " + id + " not found."),
                    HttpStatus.NOT_FOUND);
        }
        productService.deleteProduct(id);
        return new ResponseEntity<Product>(HttpStatus.NO_CONTENT);
    }

【问题讨论】:

  • 尝试将 url 更改为 url:baseUrl + "/" + product.prod_id。 @桑巴
  • @simba: 请将其标记为 aswer

标签: javascript java angularjs rest


【解决方案1】:

这可能是问题所在。当您像这样附加网址时

baseUrl + product.prod_id // let product.prod_id = 1

你会得到http://localhost:8080/webstore/product1 的结果字符串,它没有在你的后端定义。尝试将分配更改为以下内容:

baseUrl + "/" + product.prod_id

或者你只是在baseurl的末尾添加一个/。像这样:

.constant("baseUrl", "http://localhost:8080/webstore/product/")

【讨论】:

  • 如果这是问题所在,您可以在浏览器控制台中记录 REST 服务 URI 以检查它是否正确。像这样:console.log(baseUrl + product.prod_id);
  • @Muhammed Neswine 谢谢。这有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多