美文网首页
SpringBoot框架搭建以及返回数据类型为json类型示例

SpringBoot框架搭建以及返回数据类型为json类型示例

作者: 一叶知秋_zgx | 来源:发表于2018-10-27 23:27 被阅读0次
    SpringBoot是Pivotal团队提供的全新框架,它的存在减少了我们对代码的需求,前面我们在进行Spring项目搭建的时候导入了很多依赖,正是因为SpringBoot的产生让我们减少了很多依赖的注入。它最重要的是自带Tomcat服务器,让我们在启动项目的时候不用那么繁琐的去配置Tomcat的很多配置,我们只需要在其中写一个Controller方法,并且加上注解,便可以将Tomcat启动,以及让整个项目跑动起来,正是前人的聪明让我们如今在实际项目中达到了高效性。其实它就是一些库的集合,它能够被任意项目的构建系统所使用。

    以下是我查找了别人的代码并且加上自己的理解让整个项目跑动起来,亲测有效。

    这是我项目中结构:
    image.png
    pom.xml代码:
    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one
      or more contributor license agreements.  See the NOTICE file
      distributed with this work for additional information
      regarding copyright ownership.  The ASF licenses this file
      to you under the Apache License, Version 2.0 (the
      "License"); you may not use this file except in compliance
      with the License.  You may obtain a copy of the License at
    
       http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing,
      software distributed under the License is distributed on an
      "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      KIND, either express or implied.  See the License for the
      specific language governing permissions and limitations
      under the License.
    -->
    <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
      <packaging>war</packaging>
    
      <name>TestSpringBoot</name>
      <groupId>com.xingxue</groupId>
      <artifactId>TestSpringBoot</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <!-- 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>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>
    
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.7</version>
            <configuration>
              <connectors>
                <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                  <port>8888</port>
                  <maxIdleTime>30000</maxIdleTime>
                </connector>
              </connectors>
              <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
              <contextPath>/</contextPath>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    
    </project>
    
    
    Application.java代码:
    package com.xingxue;/*
     *  Created by YYH on 2018/10/27.
     */
    
    
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import com.xingxue.controller.HelloController;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @SpringBootApplication
    public class Application extends WebMvcConfigurerAdapter {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            //1.需要定义一个convert转换消息的对象;
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            //4.在convert中添加配置信息.
            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
            fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
            //5.将convert添加到converters当中.
            converters.add(fastJsonHttpMessageConverter);
        }
    
    
    
        public static void main(String[] args) {
            SpringApplication.run(HelloController.class, args);
        }
    }
    
    
    一定要记住该文件一定写在跟controller,service,dao包一个路径下,因为只有这样,它才可以扫到其中的注解,SpringApplication.run(HelloController.class, args),这句的作用就是这个。
    其次是我自己在controller中自己写的一个方法,为了让程序跑通。
    package com.xingxue;/*
     *  Created by YYH on 2018/10/27.
     */
    
    
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import com.xingxue.controller.HelloController;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @SpringBootApplication
    public class Application extends WebMvcConfigurerAdapter {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            //1.需要定义一个convert转换消息的对象;
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            //4.在convert中添加配置信息.
            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
            fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
            //5.将convert添加到converters当中.
            converters.add(fastJsonHttpMessageConverter);
        }
    
    
    
        public static void main(String[] args) {
            SpringApplication.run(HelloController.class, args);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:SpringBoot框架搭建以及返回数据类型为json类型示例

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