1

I'm trying to resolve parameters on my own.

public class NewHandler implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(Param.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        // throw Exceptions here ?

        return null;
    }
}

I can easily get input parameters from NativeWebRequest and dispatch them into corresponding customized @Param annotated params.

The problem is I also want to do some syntax check/validation in this area. But if I throw a Exception in 'resolveArgument', the full stack trace will be shown to users. That will be too excessive and unsafe. I simply want to return a JSON formatted message to users to show which input parameter syntax has errors.

Kim
  • 5,045
  • 6
  • 38
  • 60

1 Answers1

0

Delegate the actual logic of resolveArgument() into a separate method and throw an exception from there. This can then handled by the caller.

class CustomException extends Exception {}       

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
    Object output = null;

    try {
        output = actualMethod();
    }
    catch (CustomException e) {
        // do your JSON error handling here     
    }

    return output;
}

private Object actualMethod() throws CustomException {
    if (somethingBadHappened) throw new CustomException();
    else return new Object();
}

By wrapping the above try/catch construct in a loop you could also implement a repeated check (i.e. user gets the chance to correct his/her faulty input).

morido
  • 1,027
  • 7
  • 24
  • Yes, the important part actually is the "JSON error handling" there. I don't know how to output simple JSON formatted strings to users from "resolvedArgument". – Kim Jan 13 '16 at 11:27
  • @Kim Usually you would employ one of the countless JSON libs ([Genson](https://owlike.github.io/genson/), [Jackson](http://wiki.fasterxml.com/JacksonHome), [Gson](https://github.com/google/gson), ...) for such a task. – morido Jan 13 '16 at 11:40
  • Oh, I forgot you are using Spring. In that case you should have Jackson already available to you. I will update my answer with an example. – morido Jan 13 '16 at 11:49
  • Hmm... I am not a spring expert, I have to admit. But from how I understand its architecture you would indeed have to create a custom exception (as shown above) and propagate that up the call stack until you finally have the necessary context to display it to a user. Suggestions on how this can be done are [here](http://stackoverflow.com/questions/11245932/how-to-handle-exceptions-thrown-while-rendering-a-view-in-spring-mvc). Local handling (as in my example) only makes sense for logging purposes. – morido Jan 13 '16 at 12:19
  • yes, HandlerExceptionResolver, this is exactly what I am looking for! Thank you. – Kim Jan 14 '16 at 01:27