简体   繁体   English

覆盖方法上的类级别@Path注释

[英]Override class level @Path annotation on the method

I have two java files that contain endpoints that deal with file management. 我有两个包含处理文件管理的端点的java文件。 One is called FileResource.java and the other is DirectoryResource.java. 一个叫做FileResource.java,另一个叫DirectoryResource.java。 DirectoryResource.java has only one method, which is createDirectory . DirectoryResource.java只有一个方法,即createDirectory I need to move that method over to FileResource.java and remove DirectoryResource.java completely. 我需要将该方法移动到FileResource.java并完全删除DirectoryResource.java。

The problem is that the endpoint for the createDirectory method is currently /api/dir/create . 问题是createDirectory方法的端点当前是/ api / dir / create When I move it over to FileResource.java it won't work anymore because the class-level @Path annotation is at "/file/" instead of "/dir/" . 当我将它移动到FileResource.java时,它将不再起作用,因为类级别@Path注释位于“/ file /”而不是“/ dir /”

Here's my question: Is it possible to override the @Path annotation on the method so that I can to maintain the endpoint /api/dir/create after moving it to the FileResource class? 这是我的问题: 是否可以覆盖方法上的@Path注释,以便在将其移动到FileResource类之后可以维护端点/ api / dir / create?

I want to make sure that those who are using the api don't have to refactor their code to point to a new endpoint. 我想确保那些使用api的人不必重构他们的代码以指向新的端点。

//FileResource.java
...

@Path("/file/")
public class FileResource() {

  @POST
  @Path("create")
  public Response createFile(String fileContent) {
    ...
    return Response.ok().build();
  }

  ...
}


//DirectoryResource.java
...

@Path("/dir/")
public class DirectoryResource() {

  @POST
  @Path("create")
  public Response createDirectory(String path) {
    ...
    return Response.ok().build();
  }

  ...
}

There's no 'overriding' of @Path annotation. @Path注释没有“重写”。 They add . 他们补充道

Method annotated with @Path("create") in the class annotated with @Path("dir") will resolve to /dir/create . 在使用@Path("dir")注释的类中使用@Path("create")注释的方法将解析为/dir/create

You define the path by defining correct methods in correct channels. 您可以通过在正确的通道中定义正确的方法来定义路径 You move methods and delete channels only if you need to change pathes. 只有在需要更改路径时才能移动方法和删除通道。

I see no reason you need to change the channel without changing the API, but if you still need to, you should play, for example, with mod_rewrite on Apache. 我认为没有理由需要在不更改API的情况下更改频道,但如果仍然需要,则应该在Apache上使用mod_rewrite进行播放。 But I'd advise against it. 但我建议不要这样做。 Just keep your channels structure clean. 保持渠道结构清洁。

暂无
暂无

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

相关问题 JUnit @Category 方法注解覆盖类注解 - JUnit @Category method annotation to override class annotation 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) Jersey @Path 注释在类级别是强制性的 - Jersey @Path annotation mandatory at class level 使用冒号时,将类@Path注释连接到方法@Path注释 - Concatenate class @Path annotation to method @Path annotation when using colon @AspectJ 类级别的注解建议,以注解作为方法参数 - @AspectJ Class level Annotation Advice with Annotation as method argument 在运行时使用反射覆盖方法级别 @annotation,适用于类级别 - Overriding method level @annotation at runtime with reflection, works for class level 更改子类方法签名时使用@Override 注解 - Using @Override annotation when child class method signature is changed @JsonProperty 是否有类级别的注释 - Is there a class level annotation for @JsonProperty 使用AOP进行注释-在方法级别上起作用,而仅对类进行注释 - Annotation with AOP - works at method level while only the class is annotated AspectJ-在指定方法处的切入点,其参数带有类级别的注释 - AspectJ - Pointcut at specified method with a param annotated with class level annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM