编辑文章 SpringBootspringboot解析json Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回 # Sprigboot 默认使用的json解析技术框架是jackson 直接返回对象 ``` java @RequestMapping("/user") Cat getUser() { Cat cat = new User(); cat.setName("dafeiyu"); cat.setCreateDate(new Date()); return cat; } ```` 启动访问结果  # Spring Boot使用FastJson解析JSON数据 我们要使用第三方的json解析框架的话: 我们需要在pom.xml中引入相应的依赖; ``` xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency> ``` 需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架; ``` java @SpringBootApplication public class Application extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 1、需要先定义一个 convert 转换消息的对象; * 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; * 3、在convert中添加配置信息. * 4、将convert添加到converters当中. * */ // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中. converters.add(fastConverter); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 或者 @Bean注入第三方的json解析框架: ``` java @SpringBootApplication public class Application{ /** * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert * @return */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 启动 访问  <p>Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回</p> <h1>Sprigboot 默认使用的json解析技术框架是jackson</h1> <p>直接返回对象</p> <pre><code class="language-java">@RequestMapping("/user") Cat getUser() { Cat cat = new User(); cat.setName("dafeiyu"); cat.setCreateDate(new Date()); return cat; } </code></pre> <p>启动访问结果 <img src="http://img.dafeiyu.cn/file/2020/7/5db8626e3dc24da6b77423fa4da6b0bd-image.png" alt="5db8626e3dc24da6b77423fa4da6b0bd-image.png" /></p> <h1>Spring Boot使用FastJson解析JSON数据</h1> <p>我们要使用第三方的json解析框架的话: 我们需要在pom.xml中引入相应的依赖;</p> <pre><code class="language-xml"><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency> </code></pre> <p>需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架;</p> <pre><code class="language-java">@SpringBootApplication public class Application extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 1、需要先定义一个 convert 转换消息的对象; * 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; * 3、在convert中添加配置信息. * 4、将convert添加到converters当中. * */ // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中. converters.add(fastConverter); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } </code></pre> <p>或者 @Bean注入第三方的json解析框架:</p> <pre><code class="language-java">@SpringBootApplication public class Application{ /** * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert * @return */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } </code></pre> <p>启动 访问 <img src="http://img.dafeiyu.cn/file/2020/7/465dc509a7f94e0c804d51d28610f15a-image.png" alt="465dc509a7f94e0c804d51d28610f15a-image.png" /></p> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回 Sprigboot 默认使用的json解析技术框架是jackson 直接返回对象 @RequestMapping("/user") Cat getUser... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: 保存修改