【问题标题】:405(Method not Allowed) DELETE405(方法不允许)删除
【发布时间】:2014-05-19 07:54:07
【问题描述】:

问题是这样的:Spring RESTful Web 服务和一个客户端。在服务器上执行 DELETE 请求时,我得到 ->DELETE http://localhost:8080/employee/3/logout 405 (Method Not Allowed)

我已经实现了 CORS 过滤器,但它仍然不起作用。

员工控制器

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

Aa 和我的 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() {}
}

【问题讨论】:

  • 你能发布 Spring MVC 在启动时产生的输出,特别是报告控制器映射的部分吗?谢谢
  • 而不是将@requestMapping 提供给控制器,而是每次将其提供给单独的方法并从控制器中删除requestMapping。即对于 showEmployeeById 方法 @RequestMapping(method = RequestMethod.GET, value = "/employee/{employeeId}")
  • 您能否详细说明我为什么要为每种方法执行@RequestMapping?
  • 似乎除了 GET 之外没有任何方法是被允许的。尝试使用除 GET 之外的任何方法时,它会产生 405。有什么建议吗?顺便说一句,我查看了输出并映射了正确的方法。

标签: javascript ajax spring restful-url


【解决方案1】:

我遇到了同样的问题,但仍然在努力解决。问题出在 Spring Security 的某个地方,因为我的代码在没有安全性的情况下可以正常工作。但是当spring security开启时,只允许GET,PUT、POST和DELETE都不起作用。

【讨论】:

  • 你应该检查你的代码。您忘记添加的代码中可能有一些小东西。详情查看我的回答
【解决方案2】:

作为对此的回应,我的问题出在服务器端的映射上。服务器等待的内容和客户端发送的内容有问题。我正在发送名称,而服务器正在等待用户名。所以这是一个愚蠢的错误:)。如果你得到随机错误,一定要检查愚蠢的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 2013-12-25
    • 2014-12-29
    • 2018-01-25
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多