Override annotation level class @Path by method

I have two java files that contain endpoints that are related to file management. One is called FileResource.java and the other is DirectoryResource.java. DirectoryResource.java has only one method: createDirectory . I need to port this method 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 convert it to FileResource.java, it no longer works because the @Path annotation at the class level is in "/ file /" instead of "/ dir /" .

Here's my question: Is it possible to override the @Path annotation in a method so that I can maintain endpoint / api / dir / create after moving it to the FileResource class?

I want to make sure that those using the api don't have to refactor their code to point to the 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();
  }

  ...
}

      

+3


source to share


1 answer


No annotation "override" @Path

. They add .

A method annotated with @Path("create")

in a class annotated with @Path("dir")

will resolve /dir/create

.



You define the path by defining the right methods in the right channels. You move methods and delete channels only if you need to change the settings.

I see no reason why you need to change the channel without changing the API, but if you still need to, you should play with, for example, mod_rewrite

on Apache. But I would suggest doing it. Just keep the channel structure.

+3


source







All Articles