SpringBoot学习笔记一

SpringBoot特点:


  • 简化spring的开发
  • 没有xml的配置, 可以修改默认值来实现特定的需求
  • 提供大型项目中的常用的非功能特性
  • 不是对spring在功能上的增强, 而是提供了一种快速使用spring的方式

一. SpringBoot的初体验


1. 建立一个maven工程

  • 在pom.xml文件中引入spring boot父节点依赖, 引入这个之后相关的引入就不需要添加version配置, spring boot会 自动选 择最适合的版本进行添加
  • 指定jdk版本
  • 添加spring- boot-starer-web的依赖(不用添加版本号)

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.lyricgroupId>
    <artifactId>springBootartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        
        <java.version>1.8java.version>
    properties>

    <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.15version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
            <scope>truescope>
        dependency>


    dependencies>
project>

2.创建一个类

  • 添加@RestController, RestControler相当于@Controler@RequestBody
  • @RequestMapping 建立请求映射
  • 使用@SpringBootApplication来指定这是一个spring boot的应用程序

App.java

package controlcenter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

springboottest01.java

package controlcenter;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class springboottest01 {
    @RequestMapping("/hello")
    public void hello(){
        System.out.println("hello spring boot");
    }
}

3. 结果

  • 在浏览器上访问http://localhost:8080/hello, 会在控制台上面输出语句

4. 错误

Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
解决办法:
    启动类不能直接放在src目录下

二. spring boot返回json数据


步骤:

  • 新建一个测试实体类
  • 使用@RequestMapping来关联类
    @RequestMapping("/demon01")
    public Demon test01(){
        Demon demon = new Demon();
        demon.setId(22);
        demon.setName("lry");
        return demon;
    }

spring boot默认使用的json解析框架是Jackson

结果:

  • 返回一个json数组

三. Spring Boot中使用FastJson解析JSON数据


步骤:

  • 在pom文件中添加fastjson依赖

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.15version>
        dependency>
  • 配置fastjson ,两种方法

    • 第一种方法
    • 启动类继承extends WebMvcConfigurerAdapter
    • 覆盖方法configureMessageConverters
    • 需要先定义一个convert转换消息的对象
    • 添加fastjson的配置信息(是否需要格式化返回的json数据)
    • 在convert中添加配置信息
    • 将concert添加到converters当中
    @Override
      public void configureMessageConverters(List> 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);
      }
    • 第二种方法
    • 使用@Bean注入fastjsonHttpMessageConvert
    @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);
    }
  • 如果要使用第三方的json解析框架的话
    • 需要在pom.xml中引入相应的依赖
    • 需要在App.java中继承和重写方法
    • 或者室友@bean注入第三方解析框架

四 . Spring Boot热部署(springloader)


  • 构建节点
    • 添加springloader插件
  • 运行方法
    • 方式一: maven build : spring-boot:run 缺点: 任务关闭了 但是进程没有关闭
    • 方式二(推荐): 使用run as - java application
    • 把spring-loader下载下来, 放到项目的里边目录中, 然后把IDEA的run参数里VM参数设置为:•-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

五. springboot + devtools(热部署)


  • 添加相应的依赖包

    <build>
        <plugins>


            

            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <fork>truefork>
                configuration>
            plugin>
        plugins>  
    build>    

你可能感兴趣的:(SpringBoot)