【问题标题】:415 Unsupported media type error for DELETE Operation415 删除操作不支持的媒体类型错误
【发布时间】:2021-02-07 08:05:55
【问题描述】:
@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 - 有效

DELETE : 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 服务器:网络逻辑

【问题讨论】:

    标签: java rest jersey jax-rs jersey-1.0


    【解决方案1】:

    使用与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  
        }
    
    }
    

    【讨论】:

    • 感谢亚历克斯的回复。我试过了,但我得到 415 unsupported Mediatype。编辑问题
    • 如果您从您的 curl 请求中删除 --header 'Content-Type: application/json',它可能会起作用。您实际上并不需要此标头,因为它指定了请求正文内容类型,但 DELETE 请求没有任何正文。
    • 是的,即使我尝试使用或不使用该标题也没有任何区别,
    【解决方案2】:

    我不熟悉 JAX-RS。但是,我认为问题出在您使用的正则表达式@Path("{testPath: .+}") 而应该是@Path("{testPath: .*}")。并且@PathParam("testPath" 必须代替testPth: * 中的星号(*),而不是testPath 本身。

    代码中有一些字符,我不知道是不是在这里粘贴时。

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

    【讨论】:

    • 我尝试将路径参数作为 {testPath: .*} 不起作用。它给了我 405 错误。
    • 我提到了两件事,{testPath: .*} + @PathParam 名字。您使用的参数名称是什么? id?
    • 我不明白第二个。 @PathParam("testPath") String testPath 我在用同样的东西。
    • 我的意思是{testPath: .*} 表示例如testPath.idtestPath.name 等,但不是testPath。所以它将是public Response deleteValue(@PathParam("id")
    • 不,那没用。我试过这样。公共响应 deleteValue(@PathParam("id"),@PathParam("version"))
    猜你喜欢
    • 2018-10-13
    • 2018-07-25
    • 2016-09-09
    • 2022-01-24
    • 2017-08-28
    • 2021-10-12
    • 2017-10-23
    • 2014-10-02
    • 2014-06-21
    相关资源
    最近更新 更多