HTTP状态405 –不允许的方法请求方法'GET'不支持

时间:2018-12-20 16:48:07

标签: angularjs spring http spring-mvc

我正在关注使用spring,angular js,Spring REST服务的电子商务网站的教程。当我尝试使用“ MovieCartResources”类的RESTful服务时,出现错误HTTP状态405 –不允许使用方法。这是我的第一篇文章,如果我犯了一些错误,对不起。

MovieCartResources类

@Controller
@RequestMapping("/rest/cart")
public class MovieCartResources {


@Autowired
private MovieCartService movieCartService;

@Autowired
private CustomerService customerService;

@Autowired
private MovieService movieService;

@RequestMapping("/{cartId}")
public @ResponseBody
MovieCart getMovieCart(@PathVariable(value = "cartId") int cartId) {

    return movieCartService.getMovieCartById(cartId);
}

@RequestMapping(value = "/add/{movieId}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addItem(@PathVariable("movieId") int movieId, 
@AuthenticationPrincipal User activeUser) {
    Customer customer = 
customerService.getCustomerByUsername(activeUser.getUsername());
    MovieCart movieCart = customer.getMovieCart();
    Movie movie = movieService.getMovieById(movieId);

    List<MovieItem> movieItemList = movieCart.getMovieItem();

    for (int i = 0; i < movieItemList.size(); i++) {
        if (movieItemList.get(i).getMovie().getMovieId() == 
movie.getMovieId()) {
            MovieItem movieItem = movieItemList.get(i);
            movieItem.setQuantity(movieItem.getQuantity() + 1);
            movieCartService.addMovieCartItem(movieItem);
            return;
        }
    }
    MovieItem movieItem = new MovieItem();
    movieItem.setQuantity(1);
    movieItem.setMovie(movie);
    movieItem.setMovieCart(movieCart);
    movieCartService.addMovieCartItem(movieItem);
}

Angular js文件

var cartApp = angular.module("cartApp", []);

cartApp.controller("cartCtrl", function ($scope, $http) {

$scope.refreshCart = function () {
    $http.get('/eShop/rest/cart/' + $scope.movieCartId).success(function (data) {
        $scope.movieCart = data;
    });
};

$scope.clearCart = function () {
    $http.delete('/eShop/rest/cart/' + 
$scope.movieCartId).success($scope.refreshCart());
};

$scope.initCart = function (movieCartId) {
    $scope.movieCartId = movieCartId;
    $scope.refreshCart();
}

$scope.putToCart = function (movieId) {
    $http.put('/eShop/rest/cart/add/' + movieId).success(function () {
        alert("Movie successfully added to movie list!")
    });
};

$scope.removeFromCart = function(movieId){
    $http.put('/eShop/rest/cart/remove/' + movieId).success(function(){
        $scope.refreshCart();
    })
}
});

MovieCart.jsp文件

<%@include file="/WEB-INF/views/template/header.jsp" %>

<div class="container-wrapper">
<div class="container">
    <div class="jumbotron">
        <h1>Movie list</h1>
        <p>Here is your movie list</p>
    </div>
    <br>
    <section class="container" ng-app="cartApp">
        <div ng-controller="cartCtrl">
            <div>
                <a class="btn btn-danger -pull-left" ng- 
click="clearCart()">
                    <span class="glyphicon glyphicon-remove-sign"></span>
                    Clear cart
                </a>
            </div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>IMDB</th>
                    <th>Duration</th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="movieItem in movieCart.movieItem">
                    <td>{{movieItem.movie.movieName}}</td>
                    <td>{{movieItem.movie.movieType}}</td>
                    <td>{{movieItem.movie.movieIMDB}}</td>
                    <td>{{movieItem.movie.movieDuration}}</td>
                    <td><a href="#" class="label label-danger" ng- 
 click="removeFromCart(movieItem.movie.movieId)">
                        <span class="glyphicon glypicon-remove"></span>
                        remove
                    </a></td>
                </tr>
                </tbody>
            </table>
        </div>
    </section>
 </div>
 </div>

 <script src="<c:url value="/resources/vendor/bootstrap/js/controller.js" /> "></script>

 <%@include file="/WEB-INF/views/template/footer.jsp" %>

当我尝试在controller.js类中使用PUT,DELETE,GET方法时,我得到了 HTTP状态500 –内部服务器错误或HTTP状态405 –不允许的方法。我是Angular的新手,不知道如何解决此错误。在我的“ header.jsp”文件中,我有针对Angular js和Jquery的脚本。谢谢您的帮助。

0 个答案:

没有答案