• <legend id='LSPG8'><style id='LSPG8'><dir id='LSPG8'><q id='LSPG8'></q></dir></style></legend>
  • <tfoot id='LSPG8'></tfoot>
    • <bdo id='LSPG8'></bdo><ul id='LSPG8'></ul>

        <small id='LSPG8'></small><noframes id='LSPG8'>

        <i id='LSPG8'><tr id='LSPG8'><dt id='LSPG8'><q id='LSPG8'><span id='LSPG8'><b id='LSPG8'><form id='LSPG8'><ins id='LSPG8'></ins><ul id='LSPG8'></ul><sub id='LSPG8'></sub></form><legend id='LSPG8'></legend><bdo id='LSPG8'><pre id='LSPG8'><center id='LSPG8'></center></pre></bdo></b><th id='LSPG8'></th></span></q></dt></tr></i><div id='LSPG8'><tfoot id='LSPG8'></tfoot><dl id='LSPG8'><fieldset id='LSPG8'></fieldset></dl></div>

        springMVC返回复杂的json格式数据方法

        时间:2023-12-11

      1. <tfoot id='DRLKf'></tfoot>
        <i id='DRLKf'><tr id='DRLKf'><dt id='DRLKf'><q id='DRLKf'><span id='DRLKf'><b id='DRLKf'><form id='DRLKf'><ins id='DRLKf'></ins><ul id='DRLKf'></ul><sub id='DRLKf'></sub></form><legend id='DRLKf'></legend><bdo id='DRLKf'><pre id='DRLKf'><center id='DRLKf'></center></pre></bdo></b><th id='DRLKf'></th></span></q></dt></tr></i><div id='DRLKf'><tfoot id='DRLKf'></tfoot><dl id='DRLKf'><fieldset id='DRLKf'></fieldset></dl></div>

          <legend id='DRLKf'><style id='DRLKf'><dir id='DRLKf'><q id='DRLKf'></q></dir></style></legend>

              <tbody id='DRLKf'></tbody>

                <bdo id='DRLKf'></bdo><ul id='DRLKf'></ul>

                  <small id='DRLKf'></small><noframes id='DRLKf'>

                  为了返回复杂的json格式数据,我们可以在SpringMVC中使用Jackson库的ObjectMapper类,将java对象序列化为json字符串。下面是SpringMVC返回复杂的json格式数据的步骤。

                  1. 添加Jackson库依赖

                  在pom.xml文件中添加Jackson库的依赖。

                  <dependency>
                      <groupId>com.fasterxml.jackson.core</groupId>
                      <artifactId>jackson-core</artifactId>
                      <version>2.12.1</version>
                  </dependency>
                  
                  <dependency>
                      <groupId>com.fasterxml.jackson.core</groupId>
                      <artifactId>jackson-databind</artifactId>
                      <version>2.12.1</version>
                  </dependency>
                  
                  <dependency>
                      <groupId>com.fasterxml.jackson.core</groupId>
                      <artifactId>jackson-annotations</artifactId>
                      <version>2.12.1</version>
                  </dependency>
                  

                  2.编写Controller代码

                  创建一个Controller,其中的方法返回一个复杂的java对象,例如Map。在方法上使用@ResponseBody注解,该注解表示将返回值作为Http响应的主体,并用ObjectMapper将java对象序列化为json字符串。

                  示例一

                  import org.springframework.stereotype.Controller;
                  import org.springframework.web.bind.annotation.RequestMapping;
                  import org.springframework.web.bind.annotation.ResponseBody;
                  import java.util.HashMap;
                  import java.util.Map;
                  
                  @Controller
                  public class UserController {
                  
                      @RequestMapping(value="/user")
                      @ResponseBody
                      public Map<String, Object> getUserInfo() {
                          Map<String, Object> result = new HashMap<>();
                          User user = new User();
                          user.setName("John");
                          user.setAge(20);
                          result.put("user_info", user);
                          return result;
                      }
                  
                      class User {
                          private String name;
                          private int age;
                  
                          // getter and setter methods
                      }
                  }
                  

                  在上述代码中,getUserInfo()方法返回一个Map,那么SpringMVC会自动将返回值转换为json格式的字符串返回给客户端。

                  示例二

                  import org.springframework.http.MediaType;
                  import org.springframework.stereotype.Controller;
                  import org.springframework.web.bind.annotation.RequestMapping;
                  import org.springframework.web.bind.annotation.ResponseBody;
                  import com.alibaba.fastjson.JSON;
                  
                  @Controller
                  public class UserController {
                  
                      @RequestMapping(value="/user")
                      @ResponseBody
                      public String getUserInfo() {
                          User user = new User();
                          user.setName("John");
                          user.setAge(20);
                          Address address = new Address();
                          address.setProvince("Beijing");
                          address.setCity("Beijing");
                          user.setAddress(address);
                          return JSON.toJSONString(user);
                      }
                  
                      class User {
                          private String name;
                          private int age;
                          private Address address;
                  
                          // getter and setter methods
                      }
                  
                      class Address {
                          private String province;
                          private String city;
                  
                          // getter and setter methods
                      }
                  }
                  

                  在上述代码中,getUserInfo()方法返回一个String类型的json格式字符串。我们使用fastjson库进行序列化,将User对象序列化为json字符串并返回。而fastjson库由于是阿里巴巴出品,所以速度和性能都不错。

                  3.测试

                  启动项目,在浏览器中输入localhost:8080/user,就会返回json格式的字符串。

                  同时也可以通过Postman等工具来测试。

                  上一篇:解决使用httpclient传递json数据乱码的问题 下一篇:SpringMVC中Json数据格式转换

                  相关文章

                  • <bdo id='rjRY0'></bdo><ul id='rjRY0'></ul>
                  <tfoot id='rjRY0'></tfoot>
                1. <i id='rjRY0'><tr id='rjRY0'><dt id='rjRY0'><q id='rjRY0'><span id='rjRY0'><b id='rjRY0'><form id='rjRY0'><ins id='rjRY0'></ins><ul id='rjRY0'></ul><sub id='rjRY0'></sub></form><legend id='rjRY0'></legend><bdo id='rjRY0'><pre id='rjRY0'><center id='rjRY0'></center></pre></bdo></b><th id='rjRY0'></th></span></q></dt></tr></i><div id='rjRY0'><tfoot id='rjRY0'></tfoot><dl id='rjRY0'><fieldset id='rjRY0'></fieldset></dl></div>

                  <small id='rjRY0'></small><noframes id='rjRY0'>

                    <legend id='rjRY0'><style id='rjRY0'><dir id='rjRY0'><q id='rjRY0'></q></dir></style></legend>