Custom json in Spring

I configured ContentNegotiationViewResolver in the Spring 3 application, so when I call the controller with a URL that looks like **. json returns a json object using the jackson library.

If I call this method:

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

In JSON, I get:

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

my questions are: ¿is there a way to customize the name myClassList which is used in json? ¿Is json possible in this way without this variable (something like the following)?

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

Thank.

+3
source share
1 answer

org.springframework.web.servlet.ModelAndView List . modelAndView . , :

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

Source: https://habr.com/ru/post/1755697/


All Articles