2010-07-21 2 views
3

Spring 3 애플리케이션에 ContentNegotiatingViewResolver를 구성 했으므로 ** json과 비슷한 URL로 컨트롤러를 호출 할 때 jackson 라이브러리를 사용하여 json 객체를 반환합니다. 나는이 방법을 호출하면Spring의 사용자 정의 json

다음 JSON에서

@RequestMapping("/myURL.json") 
public List<MyClass> myMethod(){ 
    List<MyClass> mylist = myService.getList(); 
    return mylist; 
} 

을 나는이 나타납니다

{"myClassList":[ 
    { object 1 in json }, 
    { object 2 in json }, 
    { object 3 in json } ... 
] 
} 

내 질문

은 다음과 같습니다 ¿에서 사용되는 이름 myClassList을 구성 할 수있는 방법이 아들? ¿ 이런 식으로이 변수가없는 json이 가능합니까? (다음과 같은 것)?

[ 
    { object 1 in json }, 
    { object 2 in json }, 
    { object 3 in json } ... 
] 

감사합니다.

답변

4

List 개체 대신 직접 org.springframework.web.servlet.ModelAndView 개체를 반환 할 수 있습니다. modelAndView 객체에서 키의 이름을 설정할 수 있습니다. 다음 스 니펫을 참조하십시오.

@RequestMapping("/myURL.json") 
public ModelAndView myMethod(){ 
    ModelAndView modelAndView = new ModelAndView(); 
    List<MyClass> mylist = myService.getList(); 
    modelAndView.addObject("MyClassName", myList); 
    return modelAndView; 
} 
관련 문제