Spring MVC @ResponseBody return a List

后端 未结 7 1465
说谎
说谎 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:26

    During 2 days, i tried many ways : - responseEntity - httpheaders - XML etc...

    For a JSON (default behavior), the project need a library with all Spring library. Here the library to declare in Maven project.

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.1</version>
    </dependency> 
    

    Without this library, i have an Error (406).

    Thank you anyway for all your answers & advices.

    0 讨论(0)
  • 2021-02-10 03:31

    Yes, when your controller method in annotated with @ResponseBody, Spring transforms returned data into JSON.

    0 讨论(0)
  • 2021-02-10 03:34

    @ResponseBody will automatically encode the object you return to appropriate formats based on the Accept header of the request and the presence of JSON and/or XML libraries in the classpath.

    It can be easier/safer to define your own object to wrap the list in though rather than returning the list directly - as that gives you more control over the encoding and also allows you to potentially add other data in the future.

    0 讨论(0)
  • 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

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.7.1</version>
    

    and your method handler is here

        @ResponseBody
        @RequestMapping(value = "/your URL")
        public ArrayList<Long> getInboxPage(@RequestParam int var,HttpSession session) {
    
            ArrayList<Long> fooList=new ArrayList<Long>();
            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.

    0 讨论(0)
  • 2021-02-10 03:47

    You can build REST services using spring mvc framework. It will return JSON / XML. And call those services using HTTP clients / rest templates and use returned JSON to display information.

    Spring controllers can return an object, list of objects as well. And some mappings (Jackson and JAXB) will allow it to convert object into JSON / XML.

    If your services accept request data, you can send an object to service and get response data.

    You can use Grails frameworks as well.

    0 讨论(0)
  • 2021-02-10 03:50

    The @ResponseBody annotation tells Spring that we will be returning data in the response body rather than rendering a JSP.

    When the @ResponseBody annotation is used, Spring will return the data in a format that is acceptable to the client. That is, if the client request has a header to accept json and Jackson-Mapper is present in the classpath, then Spring will try to serialize the return value to JSON. If the request header indicates XML as acceptable (accept=application/xml) and Jaxb is in the classpath and the return type is annotated with Jaxb annotation, Spring will try to marshall the return value to XML.

    0 讨论(0)
提交回复
热议问题