springboot2.0入門(helloworld)


環境用的是jdk1.8 加上springboot 2.0  其它版本應該也差不多,現在spring boot比較火,決定學習一下,另外springcloud也是,現在很多公司微服務都是基於springcloud,把springboot學好了再學springcloud應該會輕松很多。下面開始進入正題了,代碼可以看文章最后,我貼了一份。

1,訪問http://start.spring.io/ 構建工程,如下圖

 

2,導入本地工程,用的是idea,eclipse選擇導入已存在的maven工程

 

 

 

 

3,文件修改,pom添加spring-boot-starter-web

添加pom依賴

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

 

修改測試代碼,樓主偷懶,把代碼扔在一個包了,后面有不同包的解決方案

package com.cjl.helloworld;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

 

@SpringBootApplication

@RestController

public class HelloworldApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(HelloworldApplication.class, args);

    }

 

    @RequestMapping("/")

    String home() {

        return "Hello World!";

    }

}

 

4啟動工程,訪問http://localhost:8080

補充:以下必看,避免出現訪問不了的情況

注意:如果啟動文件(HelloworldApplication)和 controller分別是兩個包路徑下,如下圖,啟動后訪問有可能訪問不到,有兩種解決方式

1,在main方法對應的類上,加上@ComponentScan(value= {"com.cjl.controller"}),這個注解可以指向包路徑,這樣就可以啟動了,如下圖

2,controller類所在的包在application啟動文件包下的子包,如下圖,這樣它啟動后能直接找到,此時不需要@ComponentScan注解

樓主小白剛開始摸索springboot,代碼自己試過,有錯誤之處請指正,新知識是一個摸索的過程,嘗試過總會有所收貨的,代碼如下:

----------------------------------------------------------------------------------------------controller

package com.cjl.controller;

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

@RestController
public class HelloController {

@RequestMapping("hello")
public String hello() {
     return "hello world";
    }
}

 

 

--------------------------------------------------------------------------------------------

package com.cjl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//@ComponentScan(value= {"com.cjl.controller"})
@SpringBootApplication
public class HelloworldApplication {

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

 

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

<name>springboot-start</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<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>
</dependencies>

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


</project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM