2012-04-28 2 views
0

요격기의 컨트롤러 메소드와 인터셉터의 메소드가 리턴 한 객체를 구현해야한다는 요구 사항이 있습니다.Spring MVC의 인터셉터에서 클라이언트가 요청할 컨트롤러 메소드를 얻는 방법

왜?

메소드에 주석이 달린 어노테이션을 사용하여 클라이언트로 리턴 할 데이터 유형을 선언하고 싶습니다.

@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"를 구현할 수

예를 들면?

+0

스프링 MVC는'@ ResponseBody'를 사용하여 자체 인터셉터를 작성할 필요없이 JSON 출력을 자체적으로 처리 할 수 ​​있습니다 – skaffman

답변