Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 如何在SpringMVC的拦截器中实现客户端请求的控制器方法_Servlets_Spring Mvc_Request_Interceptor - Fatal编程技术网

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