David Wellman - 2010-05-03

J2J has been updated to allow for better integration with Spring.

by adding the following code to your spring enabled project:

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;
    }
}

JSON is exported directly from an annotated mapped method such as:

    @RequestMapping(value = "/account/info", method = RequestMethod.GET)
    @RequestJsonBody
    public Object showAccountInformation() {
        return accountDao.getAccount();
    }