Servlets 如何在SpringMVC的拦截器中实现客户端请求的控制器方法
我需要在拦截器中实现控制器的方法以及拦截器中该方法返回的对象 为什么? 因为我想声明数据类型,该数据类型将使用方法上的注释返回给客户机。例如:Servlets 如何在SpringMVC的拦截器中实现客户端请求的控制器方法,servlets,spring-mvc,request,interceptor,Servlets,Spring Mvc,Request,Interceptor,我需要在拦截器中实现控制器的方法以及拦截器中该方法返回的对象 为什么? 因为我想声明数据类型,该数据类型将使用方法上的注释返回给客户机。例如: @Controller @Scope("prototype") @RequestMapping("/hello/") public class HelloWorld { @ResponseType(DataType.JSON) @RequestMapping(value="/{username}") public UserInfo
@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”?您试过Jackson处理器了吗
它会自动将JSON与控制器进行转换。Spring MVC支持它。Spring MVC能够使用
@ResponseBody