HTTP Post方法returnin状态代码404

时间:2017-03-08 18:19:15

标签: spring http-status-code-404 spring-restcontroller

我的maven项目使用了hibernate apache cxf spring和backbone.js 我正在尝试使用post方法将数据保存到表中。但我总是得到404作为响应代码。

我的index.html代码

    var departmentNameModel=Backbone.Model.extend({

        urlRoot:"/rest/departmentName",
        defaults:{
            departmentName:"Boş"
        }

    });

    var departmentNameView=Backbone.View.extend({

        tagName:"tr",
        template:"<td><span>{{departmentName}}</span><input type='text' value='{{departmentName}}' style='width:190px; display:none;' /><button class='btn btn-danger btn-mini' style='float:right;'>Sil</button> </td>",
        model: {},
        events:{
            "dblclick span":"duzenlemeModu",
            "blur input":"duzenle",
            "click button":"sil"
        },

        duzenlemeModu:function(){
            this.$el.find("input").css("display","");
            this.$el.find("span").css("display","none");
        },

        duzenle:function(){
            this.model.save("departmentName",this.$el.find("input").val());

            this.render();
            this.$el.find("input").css("display","none");
            this.$el.find("span").css("display","");
        },

        sil:function(){
            this.model.destroy();
            this.remove();
        },

        render: function(){
            var html=  Mustache.to_html(this.template,this.model.toJSON());

            $(this.el).html(html);
            return this;
        }


    });
    var AppView=Backbone.View.extend({

        el: $("body"),
        events:{
            "keypress #departmentName":"kaydet"
        },   
        kaydet:function(evt){
            if(evt.keyCode!==13) return;
            var departmentNameeModel=new departmentNameModel();
            departmentNameeModel.set("departmentName",$("#departmentName").val());
            departmentNameeModel.save();
            var departmentNameeView=new departmentNameView();

            departmentNameeView.model=departmentNameeModel;
            $("table").append(departmentNameeView.render().el);
            $("#departmentName").val("");

        }

    });

    var apppView = new AppView();
</script>
</body>
</html>

我的cxfservlet url-pattern是/ rest / *

我的资源/部门代码部分

    @Component
@Path("/department")
public class DepartmentResource {

    @Autowired
    private DepartmentService departmentService;

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
        public DepartmentDTO save(DepartmentDTO dto) {
          //dto.setDepartmentName();

          return departmentService.save(dto);
        }

我在屏幕截图上分享了我的错误。我的英语不太好。我希望我能告诉你这个问题。

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

至少我可以看到你的@Path(“/ department”)和urlRoot:“/ rest / departmentName”不匹配。这可能是您收到404(Not Found)错误消息的原因。尝试匹配urlRoot和@Path模式,看看是否有帮助。

提示:我建议使用postman chrome插件测试您的服务,以确保在尝试尝试从.html文件调试服务调用404响应问题之前,Rest服务是否正常工作。希望这可以帮助。