415 Unsupported media type error for DELETE Operation

1.2k views Asked by At
@Path("v2/test”)
Class Test{

    @Path(“{id}/{version}”)
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getvalue(@PathParam(“id”)
        String id, @PathParam(“version”)
        String version) {
       //do some thing
    }
  @DELETE
  @Path(“{testPath: .+}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response deleteValue(@PathParam("testPath")
      String testPath) throws Exception {
//do something  
}

}

GET : http://localhost:8080/v2/test/testId/1.0 - works

DELETE : http://localhost:8080/v2/test/testId - works

DELETE : http://localhost:8080/v2/test/testId/1.0 - 405 method not allowed error

When I add two Delete paths I get 415 error (the same curl works with no version)

@Path("v2/test”)
    Class Test{
        @Path(“{id}/{version}”)
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getvalue(@PathParam(“id”)
            String id, @PathParam(“version”)
            String version) {
           //do some thing
        }
      @DELETE
      @Path(“{id}")
      @Produces(MediaType.APPLICATION_JSON)
      public Response deleteValue(@PathParam("id")
          String id) throws Exception {
    //do something  
    }
      @DELETE
      @Path(“{id}/{version}")
      @Produces(MediaType.APPLICATION_JSON)
      public Response deleteValue(@PathParam(“id”)
            String id, @PathParam(“version”)
            String version) throws Exception {
    //do something  
    }
    
    }

curl -X DELETE --header 'Content-Type: application/json' http://localhost:8080/v2/test/testId/1.0 - gives me error 'Error 415--Unsupported Media Type'

curl -X DELETE --header 'Content-Type: application/json' http://localhost:8080/v2/test/testId - works fine(even without contentType works fine)

But If I remove Get method DELETE Operation with id and version works

@Path("v2/test”)
Class Test{

  @DELETE
  @Path(“{testPath: .+}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response deleteValue(@PathParam("testPath")
      String testPath) throws Exception {
//do something  }

}

DELETE : http://localhost:8080/v2/test/testId - works

DELETE : http://localhost:8080/v2/test/testId/1.0 - works

Could some one please help on how can I fix this ? I want get and delete methods in above mentioned format how can I achieve this ?

Jdk : 1.6 Jersey : 1.10 Server : weblogic

2

There are 2 answers

3
Alex Shesterov On

Create another DELETE-method with the same parameters as getValue:

@Path("v2/test”)
Class Test{

    @GET
    @Path(“{id}/{version}”)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getValue(
        @PathParam(“id”) String id, @PathParam(“version”) String version
    ) {
       //do something
    }

    @DELETE
    @Path(“{id}/{version}”)
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteValue(
        @PathParam(“id”) String id, @PathParam(“version”) String version
    ) throws Exception {
       return this.deleteValue("/" + id + "/" + version);
    }

    @DELETE
    @Path(“{testPath: .+}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteValue(
        @PathParam("testPath") String testPath
    ) throws Exception {
      //do something  
    }

}
8
Aman On

I am not familiar with JAX-RS. But, I think the problem is from the regex you used, @Path("{testPath: .+}") while instead it should have been @Path("{testPath: .*}"). And @PathParam("testPath" has to be what goes in place of the star(*) in testPth: *, not testPath itself.

There are some characters in the code, i don't know if it was while pasting it here though.

@Path("/v2/test") // updated
Class Test{
  @DELETE
  @Path("/{testPath: .*}") //updated - try may be '.+' also
  @Produces(MediaType.APPLICATION_JSON)
  public Response deleteValue(@PathParam("testPath") String testPath) throws Exception { //updated
     //do something  
  }
}