use*_*530 8 java spring spring-mvc spring-boot
我正在使用spring mvc来设置rest api,大多数配置都是通过spring boot项目自动设置的.在前端我使用angularjs和他们的$ http模块向服务器发出ajax请求以获取资源.资源URL在我的控制器类中定义,但只匹配GET URL.我尝试过PUT和POST,但是这些方法不允许返回405方法,而且分别禁止403方法.
我的控制器看起来像这样
@Controller
@RequestMapping("/api/users")
public class UserController {
@Inject
UserService svc;
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<User> home() {
return svc.findAll();
}
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
@ResponseBody
public User findById(@PathVariable long id){
return svc.findById(id);
}
@RequestMapping(method = RequestMethod.PUT, value="/{id}")
@ResponseBody
public User updateUser(@PathVariable long id, @RequestBody User user){
Assert.isTrue(user.getId().equals(id), "User Id must match Url Id");
return svc.updateUser(id, user);
}
}
Run Code Online (Sandbox Code Playgroud)
并且与服务器的请求不匹配的URL看起来像这样
$http({
url: BASE_API + 'users/' + user.id,
method: 'PUT',
data:user
})
Run Code Online (Sandbox Code Playgroud)
这会产生对localhost:8080/api/users/1的PUT请求,服务器会响应405 Method Not Allowed响应代码.
当服务器收到对localhost的HTTP GET请求时,正确处理相同的请求映射但使用RequestMethod.GET:8080/api/users/1
任何见解都会有所帮助.
PS以防需要包含弹簧启动依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
谢谢
fye*_*lci 12
由于csrf保护,我有同样的错误.如果启用了csrf保护,则需要在请求标头中发送csrf参数.
您也可以在此处查看Spring文档.
我将这些参数添加到我的jsp文件中
<input type="hidden" id="csrfToken" value="${_csrf.token}"/>
<input type="hidden" id="csrfHeader" value="${_csrf.headerName}"/>
Run Code Online (Sandbox Code Playgroud)
并修改了我的ajax调用,如下所示.
var token = $('#csrfToken').val();
var header = $('#csrfHeader').val();
$.ajax({
type : 'POST',
url : contextPath + "/qd/translate",
data: JSON.stringify(json),
dataType : 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader(header, token);
},
success : function(result) {
if (result.status === 'ok') {
$('#translationModal').modal('hide');
alert('Error when translating: ' + result.resultMessages.succeess);
} else {
alert('Error when translating: ' + result.resultMessages.error);
}
},
error : function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.status + " " + jqXHR.responseText);
}
});
Run Code Online (Sandbox Code Playgroud)
查看日志(DEBUG的安全性),您将发现问题.很可能你没有禁用csrf保护,而你的客户端没有发送csrf令牌.(标准Spring Security,而不是Spring Boot,但如果您使用的是Boot security autoconfig,则可以使用外部配置设置关闭csrf.)
| 归档时间: |
|
| 查看次数: |
26833 次 |
| 最近记录: |