java – How to exit the controller before reaching the end with Vraptor 3?

Question:

Is there a command in vraptor 3 that exits the controller before reaching the end of it? Because I have a controller that, if a certain process happens in the middle of it, it will return an error json, and I need it to stop continuing the code.

Below is an example:

 @Path("/metogo)
 public void Metodo(Teste teste) {

  if( aconteceAlgo  ){
     result.use(json()).indented().from(objErro).serialize();
     // Sair do controller e não continuar todo processo abaixo.
  }
  // continua o processo no Controller  
 }

NOTE: I will not redirect to any other page, just return JSON

Answer:

If a method returns void, you can terminate its execution using the word return ;

@Path("/metogo)
public void Metodo(Teste teste) {
    //...
    if (aconteceAlgo) {
        result.use(json()).indented().from(objErro).serialize();
        return; // <------------------
    }
    // continua o processo no Controller ...
}
Scroll to Top