[英]Why REST controller returns 404 status code?
I am trying to update data in my database. 我正在尝试更新数据库中的数据。 I am using jQuery/AJAX on frontend and REST/MyBatis/MySQL on backend.
我在前端使用jQuery / AJAX,在后端使用REST / MyBatis / MySQL。 Unfortunately REST controller returns 404 status code.
不幸的是,REST控制器返回404状态代码。 Please look at my REST controller code:
请查看我的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();
}
}
Here is my DocumentsMapper class code: 这是我的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);
}
And here is my AJAX method: 这是我的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);
});
});
I've just tried to switch off Spring Security and POST method became accessible. 我刚刚尝试关闭Spring Security,并且可以访问POST方法。 Here is the authorization features:
这是授权功能:
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()
;
}
I try to switch on SpringSecurity but disable CSRF .csrf().disable()
. 我尝试打开SpringSecurity但禁用CSRF
.csrf().disable()
。 After that POST methods work. 之后,POST方法开始工作。 I think that disabling the CSRF is not a good idea.
我认为禁用CSRF并不是一个好主意。 This may lead to XSS attacks.
这可能导致XSS攻击。 So I should configure CSRF-token generation and its interchange.
因此,我应该配置CSRF令牌生成及其互换。
1) Please check spring startup logs : you can get the url initialized for the project
1)请检查spring启动日志:您可以获取项目的初始化URL
2) try putting context path for the application,
2)尝试为应用程序放置上下文路径,
these link myt be helpful : in spring MVC : How to Set Context root in Spring Mvc Project in spring boot : Add context path to Spring Boot application
这些链接可能会有所帮助:在Spring MVC中: 如何在Spring Boot中在Spring Mvc Project中设置上下文根 : 在Spring Boot应用程序中添加上下文路径
sample url :
样本网址:
http://localhost:8088/ {contextPath}/documents/updateDocument
http:// localhost:8088 / {contextPath} / documents / updateDocument
3) better use API documentation like swagger ui, you can try hitting the url easly and get the available urls in your controller classes.
3)更好地使用诸如swagger ui之类的API文档,您可以尝试轻松访问URL并在控制器类中获取可用的URL。
I think you are missing @Autowired annotation in your code. 我认为您在代码中缺少@Autowired注释。 Try adding this annotation before Constructor and try again.
尝试在构造方法之前添加此批注,然后重试。 @Autowired public DocumentResources(DocumentsMapper mapper){this.mapper = mapper;}
@Autowired公共DocumentResources(DocumentsMapper映射器){this.mapper =映射器;}
您需要添加projectName以添加地址,例如: http:// localhost:8088 / projectName / documents / updateDocument
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.