• spring-boot(hello world)


     

        重拾程序,想不到从java开始,最近两周开搞web,从基本框架开始,仅做个人学习记录,遗漏之处望请海涵。

    1、基本准备

          开发环境win7;

         IDE  myeclipse Version: 2017 CI 4

         数据库mysql-5.6.26-winx64

    2、基本步骤

      (1)我们使用maven来作为依赖管理的工具,新建maven工程

          

          

          注意:create a simple project选项要勾选。

          

          选择合适的运行环境,这样就可以了。

          

          目录结构是这样的

      (2)构建pom.xml文件

          

            <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        
        <dependencies>
              <!-- web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>    

    配置好的pom.xml是这样的:

    <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.sun</groupId>
      <artifactId>spring-boot-test</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
          <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        
        <dependencies>
              <!-- web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    View Code

      (3)新建controller类

          在src/main/java中新建一个class作为启动类

      如果需要通过打包的方式在web容器中进行部署,则需要继承 SpringBootServletInitializer 覆盖configure(SpringApplicationBuilder)方法

          

    package com.sun;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class SpringbootStarter extends SpringBootServletInitializer{
        
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            builder.sources(this.getClass());
            return super.configure(builder);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootStarter.class, args);
        }
    }

    这里再写一个controller类,用来相应hello请求。

    package com.sun.controller;
    
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        
        @RequestMapping("/hello")  
        public String hello(){  
            return "Hello world!";  
        }  
    }

    这样访问http://localhost:8080/test/hello就可以看到结果了。

    需要注意的有2点

    1、有些依赖错误需要update一下maven工程。

    2、注意两个controller的打包结构,spring-boot启动加载是从启动类的根目录,

    也就是springbootstarter的所在目录com.sun开始的,如果testcontroller类不在com.sun的子目录结构下,是扫描不到的。

      

        

      

          

    欢迎大家加我qq:309620263探讨技术问题。
  • 相关阅读:
    MIT Linear Algebra#4 Determinants
    MIT Linear Algebra#3 Orthogonality
    MIT Linear Algebra#2 Vector Spaces and Subspaces
    MIT Linear Algebra#1 Solving Linear Equations
    MIT Linear Algebra#0 Introduction to Vectors
    Image Filter and Recover
    Computational Geometry
    TOP-K Problems
    pat 1151 LCA in a Binary Tree
    上传文件到git仓库中
  • 原文地址:https://www.cnblogs.com/PPBoy/p/7125898.html
Copyright © 2020-2023  润新知