chapter1 SpringBoot 入门

来源:互联网 发布:电动车拉客软件 编辑:程序博客网 时间:2024/05/22 13:45

前言


关于Spirng Boot 的学习,觉得自己JAVA WEB这块只会最原始的Servlet jsp这种东西,对做一个全栈的工程师差距还是很大的。也是最近公司环境的需要,需要自己对项目的把控,所以学习起来了。不知道坚持多久。开始吧!


从码云上看到一个教程式项目,决定跟那个一点点学起来。在参照教程的同时,记录一些自己出现的,教程上没有的问题。

    教程主页:https://gitee.com/didispace/SpringBoot-Learning

Spring Boot的主要优点教程中写到:

  • 为所有Spring开发者更快的入门

  • 开箱即用,提供各种默认配置来简化项目配置

  • 内嵌式容器简化Web项目

  • 没有冗余代码生成和XML配置的要求

SpringBoot 的好处,总结起来,少配置,多干活!

快速搭建一个基础框架

我用的IDEA 工具,很方便。后面再试试,教程介绍的方法

第一步打开IDEA后,File => New=>Project

这里写图片描述

第二步,选中Spring Initializr 选项,然后下一步

这里写图片描述

继续下一步

这里写图片描述

下一步
目前我也不太懂,先把 WEB 选上就可以了,后续估计会明白越多,需要选啥就越清楚。

这里写图片描述

下一步

这里写图片描述

点击Finish 完成了,他就会自动下载JAR包,前提IDEA MAVEN 要配置好可用,不会的,百度下IDEA MAVEN配置。

阿里 Maven 仓库地址:
http://maven.aliyun.com/nexus/content/groups/public

<mirror>      <id>nexus-aliyun</id>      <mirrorOf>central</mirrorOf>        <name>Nexus aliyun</name>      <url>http://maven.aliyun.com/nexus/content/groups/public</url>  </mirror>

工程建立完成后,结构如下图

这里写图片描述

Spring Boot的基础结构共三个文件:

  • src/main/java下的程序入口:Chapter1Application

  • src/main/resources下的配置文件:application.properties

  • src/test/下的测试入口:Chapter1ApplicationTests

接下来看一下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.wjd</groupId>    <artifactId>springbootlearning</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>Chapter1</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.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>

编写一个HelloController.java 类,提供Hello World 服务。

package com.wjd.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by Administrator on 2017/12/12. * 第一个Controller 类 */@RestControllerpublic class HelloController {    @RequestMapping("/hello")    public String index() {        return "Hello World";    }}

这个时候可以启动主程序Chapter1Application。访问 http://localhost:8080/hello ,可以看到页面输出Hello World

编写单元测试用例

这块按照教程,出了好多乱七八糟的问题,主要是版本上新版本的测试用例,和以前的有区别,主要问题是这里,评论中也有写解答方式。可以在看教程的时候注意下。目前我用的SPRING BOOT 为:1.5.6 如下写就没问题了。模拟出浏览器访问。目前我觉得多此一举。可能是对测试类书写的熟悉。

package com.wjd;import com.wjd.web.HelloController;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import static org.hamcrest.Matchers.equalTo;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@RunWith(SpringRunner.class)@SpringBootTestpublic class Chapter1ApplicationTests {    private MockMvc mvc;    @Before    public void setUp() throws Exception {        mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();    }    @Test    public void getHello() throws Exception {        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))                .andExpect(status().isOk())                .andExpect(content().string(equalTo("Hello World")));    }}

这大概就是一个Spring Boot 从无到有的一个过程。

原创粉丝点击