简体   繁体   English

415 删除操作不支持的媒体类型错误

[英]415 Unsupported media type error for DELETE Operation

@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获取:http://localhost:8080/v2/test/testId/1.0 - 有效

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

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

When I add two Delete paths I get 415 error (the same curl works with no version)当我添加两个删除路径时,出现 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 - gives me error 'Error 415--Unsupported Media Type' curl -X DELETE --header 'Content-Type: application/json' http://localhost:8080/v2/test/testId/1.0 - 给我错误“错误 415--不支持的媒体类型”

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

But If I remove Get method DELETE Operation with id and version works但是如果我删除带有 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  }

}

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

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

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?我想要上述格式的 get 和 delete 方法,我该如何实现?

Jdk: 1.6 Jersey: 1.10 Server: weblogic Jdk:1.6 Jersey:1.10 服务器:weblogic

Create another DELETE-method with the same parameters as getValue :使用与getValue相同的参数创建另一个 DELETE 方法:

@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  
    }

}

I am not familiar with JAX-RS.我不熟悉 JAX-RS。 But, I think the problem is from the regex you used, @Path("{testPath: .+}") while instead it should have been @Path("{testPath: .*}") .但是,我认为问题出在您使用的正则表达式@Path("{testPath: .+}")而不是它应该是@Path("{testPath: .*}") And @PathParam("testPath" has to be what goes in place of the star(*) in testPth: * , not testPath itself.并且@PathParam("testPath"必须是代替testPth: *中的星号 (*) 的内容,而不是testPath本身。

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  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM