Springboot入门之HelloWorld
浏览器发送Hello请求,服务器接受请求并处理,响应HelloWorld字符串
1.创建一个Maven工程;(jar)
2.导入依赖Springboot相关依赖
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.编写一个主程序:启动Spring Boot应用
@SpringBootApplication
public class HelloWorldMainApplication {
//psvm快捷键
public static void main(String[] args) {
//Spring应用启动
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4.编写相关的Contorller、Service
@Controller
public class HelloWorldContorller {
@ResponseBody
@RequestMapping
public Spring hello(){
return "Hello World";
}
}
springmvc 中@Controller和@RestController的共同点:
都是用来表示Spring某个类的是否可以接收HTTP请求
Controller, RestController的不同点:
@RestController是@Controller和@ResponseBody的结合体,两个标注合并起来的作用
@Controller
@ResponseBody
public class MyController{
}
//等价于
@RestController
public class MyController{
}
如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
如图:intellij idea刚装的时候,创建maven项目是默认没有resource文件夹的
解决:
1、点击File–>project structure…
2、Modules–>Sources–>main右键–>New Folder, 输入resources
选择resources右键,点击Resources(选择文件资源类型),然后Apply
5.运行主程序测试
6.简化部署
<!--这个插件用来打包springboot,将应用打包成一个可执行的jar包-->
<build>
<finalName>untitled</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
将这个应用打包成jar包,直接使用java -jar的命令进行执行,即使没有装tomcat环境也没问题,自带tomcat环境
Spring Boot Hello World探究
1.pom文件
1.父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
依赖的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
他是来真正管理Spring Boot所有依赖版本
Spring Boot的版本仲裁中心;
以后我们导入依赖是不需要版本的;(没有在dependencies里面管理的依赖自然需要声明版本号)
2.导入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web:
spring-boot-starter:spring-boot场景启动器;帮我们导入了web模快正常运行所依赖的组件
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的依赖
2.主程序类,主入口类
@SpringBootApplication
public class HelloWorldMainApplication {
//psvm快捷键
public static void main(String[] args) {
//Spring应用启动
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication:
Spring Boot应用标注在某个类上,说明这个类是Spring Boot的主配置类,Spring Boot就应该运行这个类的main方法来启动Spring Boot应用
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.SpringBootConfiguration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.context.annotation.ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class}), @org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter.class})})
public @interface SpringBootApplication {
@SpringBootConfiguration:Spring Boot的配置类;
标注在某个类上,表示这是一个Spring Boot配置类
@Configuration:配置类上来标注这个注释;
配置类-----配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能
以前我们需要配置的东西,Spring Boot帮我们自动配置;
@EnableAutoConfiguration告诉springboot开启自动配置功能;这样自动配置的功能才能生效