Spring MVC PUT Request returns 405 Method Not Allowed

前端 未结 6 1992
庸人自扰
庸人自扰 2020-12-19 02:27

I am using spring mvc to set up a rest api and most of the configurations are set up automatically through the spring boot project. On the front end I am using angularjs and

6条回答
  •  感情败类
    2020-12-19 03:07

    First of all, in Spring 4 you can use @RestController annotation on your rest controller class, which allows you to remove @RequestBody annotations from your methods. Makes it cleaner.

    For second, would you maybe show more of your angular code? I think you should consider using factory services to make requests to backend. For example, have a service (this service already provides post, get, delete by default):

    app.factory('User', function ($resource) {
        return $resource("/api/users/:id", {
            update: {
                method: "PUT"
            }
        });
    });
    

    Now in your Angular controller you make a new User object that you use to call this service methods.

    Query all (GET): User.query();

    Query one (GET): User.get({id: userId});

    Insert (POST): User.$save();

    Update (PUT): User.$update();

    Remove (DELETE): User.delete({id: userId}

    I've written my Angular apps like that also using Spring boot and works well.

提交回复
热议问题