The issue at hand involves a Spring RESTful web service and a client. When attempting a DELETE request on the server, the following error is encountered: ->
DELETE http://localhost:8080/employee/3/logout 405 (Method Not Allowed)
Despite implementing the CORS filter, the problem persists.
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);
}
}
The location where the request is made:
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');
});
And here's my CORS filter
@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() {}
}