代码之家  ›  专栏  ›  技术社区  ›  Rinat

为什么REST控制器返回404状态码?

  •  0
  • Rinat  · 技术社区  · 8 年前

    @RestController
    @RequestMapping("/documents")
    public class DocumentResources {
    
        private DocumentsMapper mapper;
    
        public DocumentResources(DocumentsMapper mapper) {
            this.mapper = mapper;
        }
    
        @PostMapping("/updateDocument")
        public List<Documents> updateDocument (@RequestBody Documents document) {
            mapper.updateDocument(document);
            return mapper.getAllDocuments();
        }
    }
    

    以下是我的DocumentsMapper类代码:

    @Mapper
    public interface DocumentsMapper {
        @Select("select * from documents")
        List<Documents> getAllDocuments();
    
        @Update("UPDATE documents SET title = #{title}, author = #{author}, src = #{src} WHERE id =#{id}")
        void updateDocument(Documents document);
    }
    

    下面是我的AJAX方法:

    $( "#target" ).submit(function( event ) {
    event.preventDefault();
    
    var formData = {
        id : $("#target #id").val(),    
        title : $("#target #title").val(),
        author :  $("#target #author").val(),
        src: $("#target #src").val(),
        createTime : $("#target #createTime").val(),
        editTime : $("#target #editTime").val()
    }
    
    $.ajax({
            url: 'http://localhost:8088/documents/updateDocument',
            type : "POST",
            contentType : "application/json",
            data : JSON.stringify(formData),
            dataType : 'json',
            success : function(result) {
            },
              error: function() {
                    window.location = "/error";
                    console.log('error', arguments);
                  },
                    complete: function() {
                    console.log('complete', arguments);
                  }
                }).done(function() {
                  window.location = "/documents";
                  console.log('done', arguments);
                });
      });
    

    更新

    我刚尝试关闭springsecurity,POST方法就可以访问了。以下是授权功能:

        protected void configure(HttpSecurity http) throws Exception {
    
         http
            .authorizeRequests()                                                                
                .antMatchers("/resources/**", "/signup", "/about").permitAll()
                .antMatchers("/administrator/**").hasRole("ADMIN") 
                .antMatchers("/users/**").hasRole("ADMIN")
                .antMatchers("/documents/**").hasRole("ADMIN") 
                .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")            
                .anyRequest().authenticated()                                                   
            .and()
                .formLogin()
            .and()
                .logout().logoutSuccessUrl("/login?logout") 
            .and()
                .exceptionHandling().accessDeniedPage("/403")
    //          .and()
    //              .csrf()
                    ;
    }
    

    .csrf().disable()

    3 回复  |  直到 8 年前
        1
  •  1
  •   Akhil S Kamath    8 年前

    1) 请检查spring启动日志:可以初始化url 对于项目

    这些链接很有用: 春季MVC: How to Set Context root in Spring Mvc Project 在弹簧靴中: Add context path to Spring Boot application

    示例url:

    http://localhost:8088/

    这个url很容易获得控制器类中可用的url。

        2
  •  0
  •   Steve Nash    8 年前
        3
  •  0
  •   Andrew    8 年前

    我想您的代码中缺少@Autowired注释。 请尝试在构造函数之前添加此批注,然后重试。 公共文档资源(DocumentsMapper映射器){这个.mapper=映射器;}