问题是这样的:Spring RESTful Web服务和客户端。当我在服务器上执行DELETE请求时
- > DELETE http://localhost:8080/employee/3/logout 405 (Method Not Allowed)
我已经实现了CORS过滤器,但stil不起作用。
EmployeeCotroller
@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
private static final EmployeeRepository employeeRepository = new EmployeeRepository();
public EmployeeController(){
}
@RequestMapping(method = RequestMethod.GET, value = "/{employeeId}")
public ResponseEntity<EmployeeDTO> showEmployeeById(@PathVariable int employeeId)
{
employeeRepository.setEmployeeAsActive(employeeId);
EmployeeDTO employeeDTO = employeeRepository.getEmployeeDTOForId(employeeId);
if(employeeDTO != null)
{
return new ResponseEntity<EmployeeDTO>(employeeDTO,HttpStatus.OK);
}
return new ResponseEntity<EmployeeDTO>(employeeDTO,HttpStatus.NOT_FOUND);
}
@RequestMapping(method = RequestMethod.GET, value = "/{employeeId}/viewTasks")
public ResponseEntity<List<TaskDTO>> showTasksForEmployeeId(@PathVariable int employeeId)
{
List<TaskDTO> taskDTOs = employeeRepository.getTasksForEmployee(employeeId);
if(taskDTOs != null)
{
return new ResponseEntity<List<TaskDTO>>(taskDTOs, HttpStatus.OK);
}
return new ResponseEntity<List<TaskDTO>>(taskDTOs, HttpStatus.NOT_FOUND);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/{employeeId}/logout}")
public ResponseEntity<Void> logoutEmployeeById(@PathVariable int employeeId)
{
employeeRepository.logoutEmployeeById(employeeId);
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
我提出请求的地方:
var url = "http://localhost:8080/employee/" + document.cookie1;
$('#employeeLogout').click(function(){
$.ajax({
type: "DELETE",
url: url + "/logout",
async: true
});
document.cookie1 = null;
window.location.assign('http://localhost:9000/#/employees');
});
Aaand我的CORS过滤器
@Component
public class CORSFilter implements Filter {
//Web container will call a filter when the request was made.
// CORS filter allows Cross-Origin requests-responses to be performed by adding Access-Controll-Allow-Origin in the header"
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
答案 0 :(得分:1)
同样的问题我已经过去并且仍然在努力管理。问题出在Spring Security的某处,因为我的代码在没有安全性的情况下工作正常。但是当启用Spring安全性时,只允许GET,PUT,POST和DELETE不起作用。
答案 1 :(得分:-1)
作为对此的回应,我的问题是在服务器端使用映射。服务器等待的内容和客户端发送的内容出现问题。我正在发送名称,服务器正在等待用户名。所以这是一个愚蠢的错误:)。如果你得到随机错误,一定要检查愚蠢的错误