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>