SpringBoot-Hello,Spring Boot2
结构:
1.配置pom.xml
<parent>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
<version>4.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
第一次可能会很慢,因为要下载很多依赖
2.创建主类
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
3.创建控制类
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handler01(){
return "Hello,Spring Boot2";
}
}
4.注解解释
@SpringBootApplication:告诉springboot这是一个springboot应用
@RestController是以下注解的组合注解
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Controller
- @ResponseBody:将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
@RequestMapping:在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.xml中配置