选择Spring Initializr,jdk必须选择1.8及以上版本,选择next
项目名,需要注意的是,springBoot打包方式选择jar,当需要外接tomcat,自己编写jsp界面时,选择war,然后选择next,
需要建立web项目,以及连接数据库时,进行勾选,右边就会出现配置
建立新项目后,pom.xml会根据刚才勾选的内容,自动生成Maven。
其中: starter-parent的意思为
当新建测试类时:
package com.example.demo.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:qsc
* @date: 2019/4/24
* @time: 8:36
* @Describe: springBoot
*/
@RestController
@EnableAutoConfiguration
public class IndexController {
//在微服务情况下,基本都在类上面加上@RestController ,目的?
//使用传统方式返回json(在类上面写的是Controller,不是ResController),需要在类上面加上@ResponseBody
//使用@RestController修饰类,返回的全部都是JSON,这样就不用再方法上加@ResponseBody
@RequestMapping("/index")
public String index(){
return "springBoot--index";
}
/**
* 测试main
* @param args
*/
//如何启动?使用main启动类
//@EnableAutoConfiguration作用: 开启自动装配,加载依赖信息,在本类中使用
public static void main(String[] args) {
//告诉SpringBoot,启动入口,默认端口号8080
SpringApplication.run(IndexController.class, args);
}
}
但是,实际上,当我们用IDEA建新项目的时候,会自动创建启动类Application:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//@SpringBootApplication替代@EnableAutoConfiguration + 当前包和同级包下@ComponentScan("com.example.demo")
//SpringBoot整合静态资源,默认静态资源目录位置位于classpath下。
// 目录名需符合如下规则: /static /public /resource,但是在项目启动访问文件目录时,
//在地址中,不需要加上该目录名
}
当引入JSP界面时时:首先,pom中:
在property中:
Controller
package com.example.demo.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author:qsc
* @date: 2019/4/24
* @time: 9:47
* @Describe:
*/
@Controller
public class JspController {
@RequestMapping("/jspIndex")
public String jspIndex(){
return "index";
}
}
引入全局捕获异常时:
package com.example.demo.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:qsc
* @date: 2019/4/24
* @time: 9:56
* @Describe:全局捕获异常
*/
@RestController
public class ErrorController {
//全部捕获异常,使用Aop技术去拦截,采用异常通知
//每个方法上都可能会发生异常,如果每个都用一次try,代码很冗余
@RequestMapping("/getUser")
public String getUser(int i){
try {
int j=1/i;
}catch (Exception e){
return "fail";
}
return "success";
}
}
可以新建异常处理类:
package com.example.demo.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author:qsc
* @date: 2019/4/24
* @time: 10:03
* @Describe:全局捕获异常 1,返回json格式 2,返回界面
*/
//切点,加上扫包范围
@ControllerAdvice(basePackages = "example.demo.Controller")
public class ErroeUtil {
@ResponseBody //返回json格式 //modeAndView 返回页面
@ExceptionHandler(RuntimeException.class) //运行时异常,被try-catch的地方不会被捕获
public Map<String,Object> errorJson(){
//实际上,应该将异常放在日志中
Map<String,Object> err=new HashMap<>();
err.put("errorCode","500");
err.put("errorMsg","全局捕获异常");
return err;
}
}
多环境配置文件区分: 开发,生产,测试都有自己的环境,可以自己配置,
如:
需要用到那个的时候。只需要在property中配置就好了: