实用Spring基础(1)----搭建SpringBoot项目

环境要求

1、java 7以上,下载地址

2、IntellJ IDEA Ultimate 版本(自行破解), 下载地址


新建一个maven工程





后面请直接【Next】即可,名字自定义


新建之后,打开pom.xml文件,将以下内容替换上。

<?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>com.byron</groupId>
    <artifactId>spring_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <exec.mainClass>com.gonsin.handdraw.Application</exec.mainClass>
    </properties>

    <!--spring boot 环境需要添加的内容-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <!--spring boot 环境需要添加的内容-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--spring boot 环境需要添加的内容-->
    <dependencies>
        <!--spring boot 相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mvc</artifactId>
        </dependency>

    </dependencies>

</project>

java文件夹的目录如下,照着创建就行
java

    |- com

        |- byron

            |- spring

                |- demo

                Application.java

                |- controller

                    HelloController.java


HelloController.java的内容

package com.byron.spring.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "spring", required = false) String name){
        return "hello " + name;
    }

}


Application.java 的内容

package com.byron.spring.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.byron.spring.demo")
public class Application {

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

}

  在main方法里鼠标【右键】,点击【Run Application.main()】



大概10秒后,打开浏览器,输入 http://localhost:8080/hello 点击回车。



一个最简单的基于Spring Boot 的Web应用程序就完成。



课后作业

1、请同学们自行了解以下标注的作用,以及用法。

@RestController

@RequestMapping

@RequestParam

@ResponseBody


2、默认的SpringBoot项目监听的是8080端口,怎么修改成80端口?

猜你喜欢

转载自blog.csdn.net/dwdyoung/article/details/80859530