1

See my following 4 simple examples, 2 works for xml, the other 2 does not.

//works for html, json, xml
     @RequestMapping(value = "/test", method = RequestMethod.GET)
            public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {

                ModelAndView mav = new ModelAndView();

                    TestTO test =  new TestTO("some msg", -888);
                    mav.addObject("test", test);

                    mav.setViewName("test"); //test is a jsp page

                return mav;
            }

//does not work for xml
     @RequestMapping(value = "/test", method = RequestMethod.GET)
            public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {

                ModelAndView mav = new ModelAndView();

                    ErrorTO error =  new ErrorTO("some error", -111);
                    mav.addObject("error",error );

                    TestTO test =  new TestTO("some msg", -888);
                    mav.addObject("test", test);

                    mav.setViewName("test");

                return mav;
            }

         //works for xml and json   
@RequestMapping(value = "/test3", method = RequestMethod.GET)
    public @ResponseBody ErrorTO test3(HttpServletRequest request, HttpServletResponse response) {

        ErrorTO error = new ErrorTO();
        error.setCode(-12345);
        error.setMessage("this is a test error.");
        return error;
    }

//does not work for xml
            @RequestMapping(value = "/testlist", method = RequestMethod.GET)
            public @ResponseBody List<ErrorTO> testList2(HttpServletRequest request, HttpServletResponse response) {

                    ErrorTO error =  new ErrorTO("an error", 1);
                    ErrorTO error2 =  new ErrorTO("another error", 2);
                    ArrayList<ErrorTO> list = new ArrayList<ErrorTO>();
                    list.add(error);
                    list.add(error2);
                    return list;

            }

In the two examples that can't produce xml, is it possible for configuring spring to make it work?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Bobo
  • 8,777
  • 18
  • 66
  • 85

1 Answers1

4

The two examples that don't generate XML aren't working because you have multiple top-level objects in your model. XML has no way to represent that - you need a single model object that can then be converted into XML. Similarly, a bare list cannot be converted into XML by Spring MVC.

In both cases, you need to wrap the various model objects into a single root object, and add that to the model.

JSON, on the other hand, has no issue with representing multiple top-level objects in a single document.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • That's what I thought as well. So what would be your suggestion to do that? I guess I have to create new jaxb beans to hold those various model objects, which is a pain if you think about how many beans you will end up creating. Also would you recommend to do it in the method that also products html and json or do it in a sperate method? Many thanks! – Bobo Jun 23 '11 at 14:22