ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot学习入门简易版二---springboot2.0项目创建

2019-05-12 10:38:19  阅读:248  来源: 互联网

标签:springboot2.0 springboot 启动 创建 TestController 简易版 first


2 springboot项目创建(5)

环境要求:jdk1.8+

2.1创建maven工程

Group id :com.springboot

Artifact id: springboot2.0_first_demo

Packaging: jar

2.2pom文件配置

<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.0</modelVersion>
  <groupId>com.springboot</groupId>
  <artifactId>springboot2.0_first_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(包含各种依赖信息) -->
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>
    <!-- spring-boot-starter-web springboot整合springmvc web 
      实现原理:maven依赖继承关系,相当于把第三方常用maven依赖信息,在parent项目中已封装
  -->
  <dependencies>
      <!-- 根据需要选择parent中封装的第三方框架 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <!-- 不需要写版本号,因为在parent中已封装好版本号 -->
      </dependency>
  </dependencies>
</project>

 

2.3创建测试类及启动类(6)

Springboot启动原理:使用springmvc注解方式启动(不需要xml配置文件)

2.3.1创建启动类和测试类

简洁起见,可将启动类和controller在同一个类中(一般分别创建controller类和启动类)

 

@RestController
@EnableAutoConfiguration //自动配置,根据pom文件引入的依赖信息,自动配置对应的组件
public class TestController {

    /**
     * @EnableAutoConfiguration
     *  注解作用:扫包范围,默认在当前类中
     *  相当于 @SpringBootApplication
     * @return
     */
    @PostMapping("/test")
    public String test(){
        return "springboot2.0 first application";
    }
    
    /**
     * 程序入口
     *  SpringApplication.run 相当于java代码创建内置tomcat,加载springmvc注解启动
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(TestController.class, args);
    }
}

 

2.3.2启动springboot项目

1在TestController 类中,右键-->run as或debug as--》java app或springboot app。

 

启动成功日志:

2019-04-14 22:13:51.019  INFO 16108 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

2019-04-14 22:13:51.102  INFO 16108 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

2019-04-14 22:13:51.109  INFO 16108 --- [           main] com.springboot2.first.TestController     : Started TestController in 6.615 seconds (JVM running for 9.788)

 

2 页面访问

 

http://localhost:8080/test

 报错:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Apr 14 22:15:23 CST 2019

There was an unexpected error (type=Method Not Allowed, status=405).

Request method 'GET' not supported

 

Get方式不支持,修改类中的PostMapping为RequestMapping,方便测试。

 

重启再次访问成功:

springboot2.0 first application

 

标签:springboot2.0,springboot,启动,创建,TestController,简易版,first
来源: https://www.cnblogs.com/cslj2013/p/10851287.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有