1, 用IDE创建一个Maven项目
2, 修改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>test.ljb.spring</groupId> <artifactId>spirng-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent> <dependencies> <!--web应用基本环境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 3,创建 应用执行器和控制器应用执行器
应用执行器由注解@SpringBootApplication实现,这个注解默认是集成了@Configuration、@EnableAutoConfiguration、@EnableWebMvc、@ComponentScan四个注解的功能
package test.ljb.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
控制器
package test.ljb.spring; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class ExampleController { @RequestMapping("/") String home() { return "Hello World!"; } @RequestMapping("/hello/{myName}") String index(@PathVariable String myName) { return "Hello "+myName+"!!!"; } } 注意!!!
controller 必须放在 Application所在包的下级或者和Application同级
否者会找不到controller 访问出现
This application has no explicit mapping for /error, so you are seeing this as a fallback.
4,启动应用执行器
运行Application的main方法
启动成功会在控制台看到输出。
然后访问
http://localhost:8080/
8080是默认的端口
如果需要修改端口,在src/main/resources 下加一个application.properties
添加server.port=8888(指定端口)
工程结构如下
