Validate if method exists in the webservice and customize return message

Asked

Viewed 65 times

0

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

url1 (valid): https://127.0.0.1/system/Ws/example/query 1? param1=test 1&param2=test 2

url (invalid): https://127.0.0.1/system/Ws/example/query 2

I would like the system to display a custom message when receiving a different url than url1, rather than redirecting to the page screen not found. However, how should I create a method to intercept and validate the url, and the given path 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) {
   ...
   }

1 answer

0


I found a treatment tip (unfortunately I lost the reference link) and adapted 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);
    }
}

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.