Restyling Confirmation (JBoss AS 6)

I am looking for a good user input validation scheme for Resteasy services.

Say I have this service:

@Local
@Path("/example")
public interface IExample {
  public Response doSomething ( @QueryParam("arg1") String arg1, @QueryParam("arg2") Integer arg2);
}

which I implemented:

@Stateless
public class Example implements IExample {
  @Override
  public Response doSomething ( String arg1, Integer arg2 ) { ... }
}

What is the best practice for checking arg1 and arg2?

My ideas:

  • Confirm inside the doSomething (...) method. Disadvantage: when I add some parameter (e.g. arg3) in the future, I could easily forget to check it.
  • In custom javax.servlet.Filter. Disadvantage: I cannot access arg1 and arg2, as they have not yet been analyzed by the Resteasy framework.

I came up with this concept:

public class ExampleValidator implements IExample {
  public static class ValidationError extends RuntimeException { ... }

  @Override 
  public Response doSomething ( String arg1, Integer arg2 ) {
     // here do validation. In case of failure, throw ValidationError
     return null;
  }
}

which can be used as follows:

@Stateless
public class Example implements IExample {
  @Override
  public Response doSomething ( String arg1, Integer arg2 ) {
     try { 
       (new ExampleValidator()).doSomething(arg1, arg2); 
     } catch ( ValidationError e ) {
        // return Response with 400
     }
  }
}

, IExample.doSomething, Validator - . , Resteasy NOT TO Validator , resteasy.jndi.resources resteasy.scan, ( bean ).

- ? - ?

: , , - - Resteasy? , () , (arg1, arg2)?

, ;)

+3
1

(1) , Java EE 6 Bean Framework Validation. . ,

public Response doSomething ( String arg1, Integer arg2 )

public Response doSomething ( JAXBElement<MyDomainObject> myOJaxb )

, XML JSON , .

(2) - ServletFilter.

(3) a'la Bean Validation, ( Lombok, ).

(4) - REST

@Path("users/{username: [a-zA-Z][a-zA-Z_0-9]}")

, ( , JAX-RS)

, .

, Resteasy, (3), GitHub, :)

+1

All Articles