Request: @ResponseJsonBody
Status: Beta
Brought to you by:
davewellman
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;
}
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;
}
}
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;
}
}