玩转spring boot 快速开始(1)

网友投稿 209 2023-06-20


玩转spring boot 快速开始(1)

开发环境:

IED环境:Eclipse

JDK版本:1.8

maven版本:3.3.9

一、创建一个spring boot的mcv web应用程序

打开Eclipse,新建Maven项目

选择quickstart模板

完成Maven项目的创建

参照spring的官方例子:http://spring.io/guides/gs/testing-web/

在pom.xml增加maven依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.github.carter659

spring01

0.0.1-SNAPSHOT

jar

spring01

http://maven.apache.org

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.4.2.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.github.carter659

spring01

0.0.1-SNAPSHOT

jar

spring01

http://maven.apache.org

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.4.2.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-maven-plugin

添加一个控制器文件“HomeController.java”

package com.github.carter659.spring01;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class HomeController {

@RequestMapping("/")

public @ResponseBody String index() {

return "你好,这是第一个spring boot应用";

}

}

修改App.java文件

package com.github.carter659.spring01;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

在App.java文件处,点击右键Run As运行java程序

当控制台处显示运行结果后

在浏览器输入“http://localhost:8080/”网站访问第一个spring boot的应用

二、我们如何对spring boot做单元测试?

pom.xml中增加单元测试的依赖

org.springframework.boot

spring-boot-starter-test

test

在src/test/java中新建测试类“HttpRequestTest.java”

package com.github.carter659.spring01;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.context.embedded.LocalServerPort;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

import org.springframework.boot.test.web.client.TestRestTemplate;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

public class HttpRequestTest {

@LocalServerPort

private int port;

@Autowired

private TestRestTemplate restTemplate;

@Test

public void greetingShouldReturnDefaultMessage() throws Exception {

assertThat(tqfioGtEgluhis.restTemplate.getForObject("http://localhost:" + port + "/",

String.class)).contains("你好,这是第一个spring boot应用");

}

}

并运行单元测试

出现绿色表示断言成功

三、我们怎么部署spring boot?

我们会按照以下这几个步骤:

1.下载maven

在maven的官方网站下载maven的bin包:http://maven.apache.org/download.cgi

2.配置环境变量:

我这里是把maven解压到d盘的Program Files (x86)目录

在环境变量中输入:MAVEN_HOME -->D:\Program Files (x86)\maven

Path中追加:;%MAVEN_HOME%\bin;

在CDM窗口中输入“mvn -v”命令来测试maven是否安装成功

这里显示的是3.3.9版本

3.打包

在程序所在目录(与pom.xml同级)输入“mvn package”命令:

出现“BUILD SUCCESS”则表示打包成功

会在tatget目录下出现打包后的jar文件

4.运行

在CMD中输入命令“java -jar target/spring01-0.0.1-SNAPSHOT.jar”

这时程序就部署好了,是不是发现spring boot程序不仅开发和测试非常简单,部署也非常容易?

代码下载:https://github.com/carter659/spring-boot-01.git


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java读写Excel实例分享
下一篇:Java多线程基本用法总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~