我有一个实现拦截器中的控制器的方法和拦截器中的方法返回的对象的需求。
为什么?
因为我想声明数据类型,该数据类型将使用方法上注释的注解返回给客户端。例如:
@Controller
@Scope("prototype")
@RequestMapping("/hello/")
public class HelloWorld {
@ResponseType(DataType.JSON)
@RequestMapping(value="/{username}")
public UserInfo hellowUser(@PathVariable("username") String username) {
UserInfo userInfo = new UserInfo();
userInfo.setUsername(username);
return userInfo.
}
}
然后拦截器:
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throw Exception {
Method method = getRequestedMethod();
Object result = getResultReturnedByTheMethod();
ResponseType responseType = method.getAnnotation(ResponseType.class);
DataType type = responseType.value();
swich(type) {
case DataType.JSON : writeJson(result);
case .......
...
}
}
那么,换句话说,我如何才能正确地实现"getRequestedMethod“和"getResultReturnedByTheMethod”?
发布于 2012-04-29 13:32:38
你试过杰克逊处理器吗?http://jackson.codehaus.org/
它会自动将JSON与控制器相互转换。并得到Spring MVC的支持。
https://stackoverflow.com/questions/10362057
复制相似问题