繁体   English   中英

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

[英]Why REST controller returns 404 status code?

我正在尝试更新数据库中的数据。 我在前端使用jQuery / AJAX,在后端使用REST / MyBatis / MySQL。 不幸的是,REST控制器返回404状态代码。 请查看我的REST控制器代码:

@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);
            });
  });

更新

我刚刚尝试关闭Spring Security,并且可以访问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()
                ;
}

更新

我尝试打开SpringSecurity但禁用CSRF .csrf().disable() 之后,POST方法开始工作。 我认为禁用CSRF并不是一个好主意。 这可能导致XSS攻击。 因此,我应该配置CSRF令牌生成及其互换。

1)请检查spring启动日志:您可以获取项目的初始化URL

2)尝试为应用程序放置上下文路径,

这些链接可能会有所帮助:在Spring MVC中: 如何在Spring Boot中在Spring Mvc Project设置上下文根在Spring Boot应用程序中添加上下文路径

样本网址:

http:// localhost:8088 / {contextPath} / documents / updateDocument

3)更好地使用诸如swagger ui之类的API文档,您可以尝试轻松访问URL并在控制器类中获取可用的URL。

我认为您在代码中缺少@Autowired注释。 尝试在构造方法之前添加此批注,然后重试。 @Autowired公共DocumentResources(DocumentsMapper映射器){this.mapper =映射器;}

您需要添加projectName以添加地址,例如: http:// localhost:8088 / projectName / documents / updateDocument

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM