bmorrise
bmorrise

Reputation: 45

Override class level @Path annotation on the method

I have two java files that contain endpoints that deal with file management. One is called FileResource.java and the other is DirectoryResource.java. DirectoryResource.java has only one method, which is createDirectory. I need to move that method over to FileResource.java and remove DirectoryResource.java completely.

The problem is that the endpoint for the createDirectory method is currently /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/".

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?

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.

//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();
  }

  ...
}

Upvotes: 3

Views: 2390

Answers (1)

Cjxcz Odjcayrwl
Cjxcz Odjcayrwl

Reputation: 22847

There's no 'overriding' of @Path annotation. They add.

Method annotated with @Path("create") in the class annotated with @Path("dir") will resolve to /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. But I'd advise against it. Just keep your channels structure clean.

Upvotes: 3

Related Questions