SpringBoot入门

来源:互联网 发布:搭建php运行环境 编辑:程序博客网 时间:2024/06/05 02:06

创建一个Maven Project





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.imooc</groupId><artifactId>SpringBootDemo</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.4.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><!-- 设置字符集,jdk版本 --><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><!-- 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-test</artifactId><scope>test</scope></dependency><!--热部署.jar ,当你修改代码后,服务器会自动重启-->      <dependency>             <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <!-- <version>1.5.4.RELEASE</version> -->        </dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!-- 热部署的的配置,只有配置了此属性,才能restart --><fork>true</fork></configuration></plugin></plugins></build></project>

项目会有一个红×,update项目,再编写一个controller和App运行程序代码


package com.imooc.chaihuo.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloHandler {@RequestMapping("/hello")public String hello(){return "Hello SpringBoot!!!";}}


主程序

package com.imooc.chaihuo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App {public static void main(String[] args) {          SpringApplication.run(App.class, args);}}

运行结果



注意:

Controller必须在App的同包或者子包下

url:localhost:8080/hello


原创粉丝点击