【问题标题】:HTTP Post method returnin status code 404HTTP Post 方法返回状态码 404
【发布时间】:2017-07-29 11:16:27
【问题描述】:

我的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);
        }

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

感谢您的回答!

【问题讨论】:

  • 您使用的是没有 MVC 模块的 Spring Framework?
  • 是的,我没有使用 SpringMVC 我使用 Backbone.js 进行分层架构

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


【解决方案1】:

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

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

【讨论】:

  • 我尝试了你的建议,但这个错误仍然存​​在 :( .My apache cxf url pattern /rest/* 所以我的 webapp 正在运行 /rest/
猜你喜欢
  • 1970-01-01
  • 2021-09-19
  • 2019-02-24
  • 2015-01-12
  • 2021-01-13
  • 2016-09-16
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多