美文网首页
SpringBoot 搭建+返回值转json

SpringBoot 搭建+返回值转json

作者: 海棠晴yyh | 来源:发表于2018-10-27 12:56 被阅读0次

    添加依赖pox.xml:

    <!-- Spring Boot 启动父依赖 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
      </parent>
    
      <dependencies>
        <!-- Spring Boot web依赖 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
        </dependency>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-configuration-processor</artifactId>
              <optional>true</optional>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>fastjson</artifactId>
              <version>1.2.15</version>
          </dependency>
      
      </dependencies>
    

    RecruitController.java:

    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    
    @EnableAutoConfiguration
    @RestController
    public class RecruitController {
        @RequestMapping("/")
        public HashMap<String,Object> sayHello() {
            HashMap<String,Object> map=new HashMap<>();
            map.put("id",10);
            map.put("name","张三");
            return map;
        }
    }
    }
    

    创建启动类Application.java:

    package com.xingxue;/*
     *  Created by YYH on 2018/10/29.
     */
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @SpringBootApplication
    public class Application {
    //返回值转json
        @Bean
        public HttpMessageConverters fastJsonHttpMessageConverters(){
            //1.需要定义一个convert转换消息的对象;
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            //2:添加fastJson的配置信息;
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            //4.在convert中添加配置信息.
            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
            fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
            HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
            return new HttpMessageConverters(converter);
    
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Application .class, args);
        }
    }
    

    补充:
    var json= JSON.stringify(data);
    alert(json);

    相关文章

      网友评论

          本文标题:SpringBoot 搭建+返回值转json

          本文链接:https://www.haomeiwen.com/subject/aycdtqtx.html