Spring MVC @ResponseBody return a List

后端 未结 7 1607
说谎
说谎 2021-02-10 03:22

We would like to create a \"WebService\" which return a list of specific objects. And we would like to call this webservice from another java program by apache http clients libr

7条回答
  •  轮回少年
    2021-02-10 03:42

    Actually you have to use REST web service that carry JSON/XML format as Objects representation. I prefer JSON because this is very light weight.

    First you need to add dependency in your Pom.xml

    
    org.codehaus.jackson
    jackson-mapper-asl
    1.7.1
    

    and your method handler is here

        @ResponseBody
        @RequestMapping(value = "/your URL")
        public ArrayList getInboxPage(@RequestParam int var,HttpSession session) {
    
            ArrayList fooList=new ArrayList();
            fooList.add(1L);
            fooList.add(2L);
            fooList.add(3L);
    
            return fooList;
    
        }
    

    NOTE: Spring automatically make JSON if you write @ResponseBody annotation in your method handler you don't need to add Jackson dependency in your pom.xml file.

提交回复
热议问题