springboot

2.快速搭建第一个springboot应用

2020-03-11  本文已影响0人  0f701952a44b
1.创建maven项目,其中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.pp</groupId>
  <artifactId>springboot1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <!-- 继承默认值为Spring Boot -->
  <parent> 
    <groupId> org.springframework.boot </groupId> 
    <artifactId> spring-boot-starter-parent </artifactId> 
    <version> 2.1.4.RELEASE </version> 
  </parent>
  
  <!--添加Web应用程序的典型依赖项  -->
  <dependencies> 
    <dependency> 
        <groupId> org.springframework.boot </groupId> 
        <artifactId> spring-boot-starter-web </artifactId> 
    </dependency> 
    
    <!-- 热部署,开发更改代码后自动重启 -->
    <dependency> 
        <groupId> org.springframework.boot </groupId> 
        <artifactId> spring-boot-devtools </artifactId> 
        <optional> true </optional> 
    </dependency> 
</dependencies>


<!-- 将应用打成一个可执行的jar包 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>
2.创建HelloWorldApplication
package com.pp.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
 * @SpringBootApplication标注一个主程序类,说明这是一个spring boot应用
 */
@SpringBootApplication
public class HelloWordApplication {
    public static void main(String[] args) {    
       /*
    * SpringApplication.run将spring启动起来
     */
        SpringApplication.run(HelloWordApplication.class, args);
    }
}
3.创建controller service
package com.pp.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello*")
    public String hello() {
        return "hello world 第一个spring boot程序";
    }
}
4.部署
5.访问

浏览器输入地址http://localhost:8080/hello之类地址即可收到回复"hello world 第一个spring boot程序"

上一篇下一篇

猜你喜欢

热点阅读