3
\$\begingroup\$

Consider two classes:

First

@Path("/first/{param}") // 1st line
public class First {

    @GET
    @Path("/{data}/difference") // 5th line
    public Response doStuff(
            @Context HttpServletRequest request,
            @Context UriInfo uri,
            @Context HttpHeaders httpHeaders,
            @PathParam("param") String param,
            @PathParam("data") String data
    ) {

        // whatever
        return new Response();

    }

}

Second

@Path("/second/{param}") // 1st line
public class Second extends First {

    @GET
    @Path("/{data}") // 5th line
    public Response doStuff(
            @Context HttpServletRequest request,
            @Context UriInfo uri,
            @Context HttpHeaders httpHeaders,
            @PathParam("param") String param,
            @PathParam("data") String data
    ) {

        return super.doStuff(
            request,
            uri,
            httpHeaders,
            param,
            data
        );

    }

}

The only differences between this two block of code are:

  • @Path() annotation value in the 1st line of both of them
  • @Path() annotation value in the 5th line of both of them

How can I remove all the redundant code from it?

\$\endgroup\$
1
  • \$\begingroup\$ There is more different: return super.doStuff and extends for example. What problem are you trying to solve? \$\endgroup\$ Commented Feb 22, 2018 at 9:34

1 Answer 1

3
\$\begingroup\$

You can parameterize the @path annotation with regular expressions to specify one class/method that accepts multiple paths

@Path("/{firstpath: first|second}/{param}") // will match /first/{param} and /second/{param}
public class First {

    @GET
    @Path("/{data}{p:/?}{difference:(\w*)}") // {p:/?} makes "/" optional, {difference:(\w*)} allows for optional text
    public Response doStuff(
            @Context HttpServletRequest request,
            @Context UriInfo uri,
            @Context HttpHeaders httpHeaders,
            @PathParam("param") String param,
            @PathParam("data") String data
    ) {

        // whatever
        return new Response();

    }

}
\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.