[英]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.