0

Here is the code i am using for delete

@Path("/studentnames")
public class StudentResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Student> getAllStudents() {
    return StudentService.getAllStudents();
}


@GET
@Path("/{studentId}")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudent(@PathParam("studentId") int id) {
    return StudentService.getStudent(id);
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Student> addStudent(Student student){
    StudentService.addStudent(student);
    return StudentService.getAllStudents();
}

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Student updateStudent(Student student) {
    return StudentService.updateStudent(student);
}

@DELETE
@Path("/{studentId}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studentId") int id) {
    StudentService.deleteStudent(id);
  }
}   

Its working fine for all Http methods except DELETE. Here is the error if i request using delete method.enter image description here Please help me out this.

maneesh
  • 216
  • 3
  • 14
  • I tried it. Still getting same error. – maneesh May 04 '18 at 16:14
  • Please add all of your resource methods. It's possible that you're actually calling a different endpoint. – avojak May 04 '18 at 16:47
  • Yes i have updated all resources. But its working fine for all except DELETE. – maneesh May 04 '18 at 16:58
  • Do you have any other resource classes? – avojak May 04 '18 at 17:42
  • You might also try removing the @Produces annotation for your delete resource, since it doesn't actually produce a JSON response. Or change the resource to return a response. But that may not actually fix anything. – avojak May 04 '18 at 17:48
  • Yeah I tried that but it didn't fix. – maneesh May 04 '18 at 17:55
  • This is probably something that we would need to see a complete project to be able to run and test. Whatever the problem is, it is with something that you are _not_ showing us. If you can put together a minimal repo that reproduces the problem and put it on GitHub, we may be able to help you better. – Paul Samsotha May 04 '18 at 20:48
  • 1
    Also, a shot in the dark, but could [this](https://stackoverflow.com/a/25436329/2587435) be the issue? – Paul Samsotha May 04 '18 at 20:51
  • @PaulSamsotha I think that is unrelated since a 403 error would mean that the DELETE method is not even reached. However, the OP is getting a 405 error, meaning that the server can locate that DELETE method, but there is something about it that it doesn't like. As you suggest, I think a MCVE would be very helpful. – skomisa May 06 '18 at 05:43
  • @skomisa I've never used the Tomcat CarsFilter so I am not sure about its behavior. It's possible if that you don't specify the DELETE method in the allowed methods, that it would cause a 405. There's no mention of the status code the answer poster received, even though the question is about a 403. People post answers all the time that don't directly answer the OP questions, but just answer the title question. It happens. Again, like I said though, it was just a shot in the dark; but totally possible, – Paul Samsotha May 06 '18 at 16:43
  • Could you update your post with a Postman screen shot showing the Request and Response Headers in Postman Console? – skomisa May 07 '18 at 18:26

0 Answers0