Spring Boot 超·简单demo

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 访问出现

Whitelabel Error Page

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(指定端口)


工程结构如下



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值