SpringBoot

一、SpringBoot入门

1. SpringBoot简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

2. SpringBoot特性

 

2.1 SpringBoot并不是对Spring功能上的增强,而是提供了一种快速创建独立的Spring应用程序的框架

2.2嵌入的Tomcat,无需部署WAR文件

2.3 简化Maven配置

2.4 自动配置Spring

2.5 绝对没有代码生成和对XML没有要求配置

2.6备受关注,是下一代框架,已经是不争的事实,不需要学习springmvc

2.7微服务的入门级微框架,springboot是springcloud的基础

 SpringBoot_第1张图片

 

 

 

3 SpringBoot开发环境准备

3.1开发环境JDK1.8  Tomcat7.0(这里不演示配置)

3.2开发工具Eclipse或者是Idea

3.3项目管理工具Maven

 

4 SpringBootHelloWorld案例

 

4.1导入依赖

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

 

 

 

4.2创建Controller

@RestController
@RequestMapping("/first")
public class HelloController {
    @RequestMapping("/firstRequest")
    public String firstRequest(){
        System.out.println("第一个Controller请求");
        return "Hello Soringboot";
    }
}

 

 

4.3启动程序

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

 

 

4.4运行结果

 SpringBoot_第2张图片

 

 SpringBoot_第3张图片

 

 

 

 

 

 

5 SpringBoot如何不显示错误异常

5.1创建带有异常的Controller

@RestController
@RequestMapping("/first")
public class HelloController {
    @RequestMapping("/firstRequest")
    public String firstRequest(){
        int result=5/0;
        System.out.println("第一个Controller请求");
        return "Hello Soringboot";
    }
}

 

 

 

5.2创建异常类

@ControllerAdvice
public class ExceptionHandler {
    //捕获运行时异常
    @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map exceptionHandler(){
        Map  map=new HashMap<>();
        map.put("error","500");
        map.put("msg","您好 ,服务器暂时出现异常,请稍后重试");
        return map;
    }
}

 

 

 

5.3启动程序

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

 

 

5.4运行结果

 SpringBoot_第4张图片

 

 

 

6. SpringBoot使用FreeMarker

6.1创建配置文件application.properties

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request

 

 

 

6.2向页面传单个值Controller

@Controller
@RequestMapping("/free")
public class FreeController {
    @RequestMapping("/firstFree")
    public String firstFree(ModelMap map){
        map.put("name","张三");
        return "HelloFreeMarker";
    }

 

 

 

 

6.3页面

 SpringBoot_第5张图片

 

 

 

 

6.4运行结果

 SpringBoot_第6张图片

 

 

 

 

 

6.5向页面传集合Controller

@RequestMapping("/secondFree")
public String secondFree(ModelMap map){
    List list =new ArrayList<>();
    list.add("张三");
    list.add("李四");
    list.add("王五");
    map.put("names",list);
    return "HelloFreeMarker";
}

 

 

 

 

6.6页面

 SpringBoot_第7张图片

 

 

 

 

6.7运行结果

 SpringBoot_第8张图片

 

 

 

6.8向页面传对象Controller

@RequestMapping("/thirdFree")
public String thirdFree(ModelMap map){
    List studentList=new ArrayList<>();
    Student student=new Student();
    student.setStu_name("张三");
    studentList.add(student);
    map.put("student",studentList);
    return "HelloFreeMarker";
}

 

 

 

6.9页面

 SpringBoot_第9张图片

 

 

 

 

6.10运行结果

 SpringBoot_第10张图片

 

 

 

你可能感兴趣的:(SpringBoot)