Validate if method exists in webservice and customize return message

0

I am creating a webservice rest, and it is working properly, but there is a situation that I could not handle: if the url provided is searching for a non-existent method, the requester is redirected to the error screen of my system, I would like to display a custom message. Following description:

url1 (valid): link

url (invalid): link

I would like to receive a url other than url1, the system would display a custom message instead of redirecting it to the page not found. But how do I create a method to intercept and validate the url, where the path entered does not exist in my class?

@Path("/ws/exemplo")
public class exemploResource {

    //único método implementado atualmente
    @GET()
    @Produces("text/plain")
    @Path("consulta1")
    public String consulta1(@QueryParam("param1") final String param1,
            @QueryParam("param2") final String param2,
            final @Context HttpServletRequest request) {
   ...
   }
    
asked by anonymous 27.05.2016 / 19:41

1 answer

0

I found a treatment tip (unfortunately I lost the reference link) and adapted it for my use, creating the following method:

@GET()
@Produces("text/plain")
@Path("{url}")
public String validaServico (@PathParam("url") final String url) {
    //metodosExistentes é uma lista com todos os métodos existentes
    if (this.metodosExistentes.contains(url))
        return "";
    else
    {
        //omiti parte do código de setar mensagem e criar xml de retorno 
        //por não ser pertinente ao assunto
        erro.setDeErro("Consulta desejada não encontrada.");
        return XmlUtil.objectToXml(erro);
    }
}

See that the first condition exists only to maintain the structure of the validation method, because if the url is valid (that is, there is a method), the correct method will be called and my validation method will not be triggered.

    
27.05.2016 / 20:53