SpringBoot入门系列(一)-------入门实例

版权声明:原创 https://blog.csdn.net/rambler_designer/article/details/89021430

介绍 :

用SSM时间长了越来越发现ssm的弊端,大量配置文件,搭建环境比较麻烦

而相较于SSM,springboot提供给用户开箱即用的体验,因此开始慢慢接触Spring Boot

提供一个官方的例子,供大家参考

本文参考Spring Boot文档,英文还可以的建议看这个

本项目Git地址:https://github.com/spring-guides/gs-rest-service.git

推荐使用git clone代码,自己研究一下

git clone https://github.com/spring-guides/gs-rest-service.git

本项目环境:

  • JAVA JDK1.8+
  • maven3.3 (至少3.2+)
  • idea (或者Eclipse也可)

创建项目推荐两种方式

一、直接创建maven项目

创建maven项目后,手动将POM依赖导入,好处,可以自己选择maven版本及选用用户自定义设置settings.xml,可以使用自定义的镜像下载所需jar包及源码【还不太清楚直接创建maven项目如何更改maven设置....】

二、通过spring initializer方式创建

和第一种方式基本相似,只是无法自己选择maven的settings.xml中的设置

提供一下Pom文件的依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

推荐项目结构:

既然是入门的例子,就不使用JavaBean了,直接定义SpringMVC的控制器吧

package com.rambler.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Article")
public class ArticleController {

    @RequestMapping("/getArticle")
    public String B(){
        return "请求文章分类";
    }
}

注意我的项目结构,这个控制器是在com.rambler.controller包下的

定义一个启动入口,由于springboot内部集成了tomcat,因此只需要一个程序入口即可启动服务,此处有两种方式,

第一种方式,直接在当前控制器启动(不推荐)

package com.rambler.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;

@RestController
@EnableAutoConfiguration
@RequestMapping("/Article")
public class ArticleController {

    @RequestMapping("/getArticle")
    public String B(){
        return "文章列表";
    }

    public static void main(String[] args) {
        SpringApplication.run(ArticleController.class,args);
    }
}

就是在当前类上加入@EnableAutoConfiguration注解,然后在通过main方法启动当前项目(不推荐,无法自动扫描控制器的包等等)

第二种,单独在com.rambler根包下创建一个名为App或者Main的类,用于启动当前项目

package com.rambler;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

通过main方法启动服务器

启动结果如下,springboot控制台分成两栏,在右边可以看到具体信息

根据语句Mapped "{[/Article/getArticle]}"这一行,可以知道,映射请求成功

下面是一些别的映射路径

Tomcat started on port(s) 80(因为我配置了端口是80),没配置则默认8080

服务器启动成功,此时可以访问http://localhost:8080/Article/getArticle查看结果

猜你喜欢

转载自blog.csdn.net/rambler_designer/article/details/89021430