Menu

#3 Request: @ResponseJsonBody

v1.0 (example)
open-accepted
nobody
None
5
2010-05-03
2010-04-25
Anonymous
No

Can you add a spring-ish tag to replace springs @ResponseBody with a @ResponseJsonBody so all I have to do is annotate the controller and then pass back the object.

For example:

@ResponseJsonBody
@RequestMapping(value = "/doit", method = RequestMethod.GET)
public Object doStuff() {
Account account = accountDao.findAccountById(99999);
return account;
}

Discussion

  • David Wellman

    David Wellman - 2010-05-03

    public class JsonResultResolver implements ModelAndViewResolver {

    private final Log log = LogFactory.getLog(this.getClass());

    @SuppressWarnings("unchecked")
    public ModelAndView resolveModelAndView(Method handlerMethod, Class handlerType, Object returnValue, ExtendedModelMap implicitModel, NativeWebRequest webRequest) {

    boolean isSimpleJsonObject = returnValue.getClass().isAnnotationPresent(SimpleJsonObject.class);
    RequestJsonBody jsonBody = handlerMethod.getAnnotation(RequestJsonBody.class);

    if (!isSimpleJsonObject || jsonBody == null) {
    return UNRESOLVED;
    }

    ServletResponse out = (ServletResponse) webRequest.getNativeResponse();
    // out.setContentType("application/json; charset=utf-8");
    try {
    if (jsonBody.pretty()) {
    out.getWriter().write(new JsonWriter(returnValue).toString(JsonWriter.PRETTY_PRINT));
    }
    else {
    out.getWriter().write(new JsonWriter(returnValue).toString());
    }
    }
    catch (IOException e) {
    throw new RuntimeException(e);
    }

    return null;
    }

    public boolean supports(Class<?> clazz) {
    log.debug(clazz);
    return false;
    }

    }

     
  • David Wellman

    David Wellman - 2010-05-03
    • status: open --> closed
     
  • David Wellman

    David Wellman - 2010-05-03
    • milestone: --> v1.0 (example)
    • status: closed --> closed-accepted
     
  • David Wellman

    David Wellman - 2010-05-03
    • status: closed-accepted --> open-accepted
     
  • David Wellman

    David Wellman - 2010-05-03

    public class JsonResultResolver implements ModelAndViewResolver {

    private final Log log = LogFactory.getLog(this.getClass());

    @SuppressWarnings("unchecked")
    public ModelAndView resolveModelAndView(Method handlerMethod, Class handlerType, Object returnValue, ExtendedModelMap implicitModel, NativeWebRequest webRequest) {

    boolean isSimpleJsonObject = returnValue.getClass().isAnnotationPresent(SimpleJsonObject.class);
    RequestJsonBody jsonBody = handlerMethod.getAnnotation(RequestJsonBody.class);

    if (!isSimpleJsonObject || jsonBody == null) {
    return UNRESOLVED;
    }

    ServletResponse out = (ServletResponse) webRequest.getNativeResponse();
    // out.setContentType("application/json; charset=utf-8");
    try {
    if (jsonBody.pretty()) {
    out.getWriter().write(new JsonWriter(returnValue).toString(JsonWriter.PRETTY_PRINT));
    }
    else {
    out.getWriter().write(new JsonWriter(returnValue).toString());
    }
    }
    catch (IOException e) {
    throw new RuntimeException(e);
    }

    return null;
    }

    public boolean supports(Class<?> clazz) {
    log.debug(clazz);
    return false;
    }

    }

     

Log in to post a comment.