首页 > 解决方案 > 415 删除操作不支持的媒体类型错误

问题描述

@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 - 有效

删除:http://localhost:8080/v2/test/testId - 有效

删除:http://localhost:8080/v2/test/testId/1.0 - 405 方法不允许错误

当我添加两个删除路径时,我得到 415 错误(相同的 curl 没有版本)

@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 - 给我错误'Error 415--Unsupported Media Type'

curl -X DELETE --header 'Content-Type: application/json' http://localhost:8080/v2/test/testId - 工作正常(即使没有 contentType 工作正常)

但是如果我删除带有 id 和版本的 Get 方法 DELETE 操作有效

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

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

}

删除:http://localhost:8080/v2/test/testId - 有效

删除:http://localhost:8080/v2/test/testId/1.0 - 有效

有人可以帮忙解决这个问题吗?我想要上述格式的获取和删除方法,我该如何实现?

Jdk:1.6 泽西岛:1.10 服务器:weblogic

标签: javarestjerseyjax-rsjersey-1.0

解决方案


使用与以下相同的参数创建另一个 DELETE 方法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  
    }

}

推荐阅读