第二章:Spring Boot入门例子

  • 官网下载spring案例

1、访问:https://projects.spring.io/spring-boot/

2、找到如下:

175142_S7g6_142761.png

3、点击  start.spring.io ,进入如下图:

175749_0X9q_142761.png

点击“Generate Project” 按钮,下载

  •  将下载的maven工程导入到idea中

1、点击菜单的“File”->“new”->“Module from Existing Sources...”。

180305_y5YA_142761.png

2、在打开的弹框中,选择你的项目的所在路径。注意:一定要选中pom.xml文件才可以。然后点击OK。

180445_JmB2_142761.png

3、在弹框中所有的选项都不用东,直接点击“Next”按钮。

180527_pt0r_142761.png

4、最后点击“Finish”按钮。

180701_9PQC_142761.png

5、导入成功之后的maven结构如下图:

180915_7leS_142761.png

  • pom.xml

  • <?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.black.example</groupId>
    	<artifactId>springBoot-helloworld</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>springBoot-helloworld</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.2.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-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>
    

     

  • 启动Spring Boot

快捷键“ctrl+shift+F10”,运行“HelloworldApplication”,如下图,说明正常启动

181744_7Swc_142761.png

出现如下图的端口号和spring Boot使用的版本号,说明启动成功

181619_8h2j_142761.png

  •  测试类

184246_3kKA_142761.png

代码

package com.black.example.helloworld;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {

	@Test
	public void contextLoads() {
	}

}
  • 写一个简单的Http接口

创建   web包,在包下创建“HelloWorldHandler”

183634_Gjp6_142761.png

HelloWorldHandler代码如下

package com.black.example.helloworld.web;

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

/**
 * Created by 10250H on 2018/5/15.
 */
@RestController
public class HelloWorldHandler {
    @RequestMapping(value = "/index.html")
    public String index(){
        return "Hello Black!!!!!";
    }
}

重启Spring Boot服务,浏览器访问地址

http://localhost:8080/index.html    如下图:

183927_QyvQ_142761.png

接口配置成功,Boot 就是如此简单~

转载于:https://my.oschina.net/zupengliu/blog/1813049

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值