SpringBoot Series (a) helloworld!

 

  What is the most popular to say, at this stage is certainly Springboot and Springcloud, a springboot is the first time in Spring official website, showing the degree of attention springboot. The main reason springboot integrates all framework, reduce configuration, adhering to the "principles of customary over configuration" to reduce the large number of complex configuration, greatly improving efficiency.

  Creating SpringBoot project:

Environment: jdk 1.8 maven springboot eclipse

First, we chose to create a maven project

  Select an item to save the default address, click next

  Select maven-archetype-quickstart, click next

  After setting group Id and artifact Id setting, click finish

  Pom.xml jar package file import dependence, springBoot relevant package, as follows:

<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.test</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot</name>
  <url>http://maven.apache.org</url>
 <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
     <!--支持热启动 -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
         </dependency>
  </dependencies>
   <build>
    <pluginManagement>
      <plugins>
       <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eluder.coveralls</groupId>
                <artifactId>coveralls-maven-plugin</artifactId>
                <version>4.3.0</version>
            </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

  

  Pom.xml  设置好下载完成之后,写一个main函数,用于启动项目,一定要加上@SpringBootApplication注释,方法体内用SpringApplication.run()启动,注意参数的填写!

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

  

  启动类完成之后我们写一个测试类,SpringBoot中添加控制层的注释默认是@RestController,用@controller也可以,测试类如下,

/**
 * 
 *@author pillar
 *@date 2019-05-27
 */
@RestController
public class testHello {
    @RequestMapping("/hello")
    public String hello(){
        return "hello world!";
    }

}

  

  然后run as 运行项目 显示如下就运行成功了,当然Tomcat端口默认是8080,如果有冲突,你可以自己修改,这里我改为8081

  怎么修改默认端口,其实很简单,在src->main->resources->添加application.properties(application.yml)配置文件中添加server.port为8081

  启动项目之后在浏览器中输入地址,成功的显示出来!

注意:

如遇到这个错误,可能你的测试类包名范围超过你的启动类,启动类的main函数范围一定要超过你编写的代码。

 

  至此一个SpringBoot demo就完成了!

  如有不当和错误之处,请指出,我们一起交流学习,共同进步!谢谢!

Guess you like

Origin www.cnblogs.com/zhang-dongliang/p/10936470.html