源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

玩转spring boot 快速开始(1)

  • 时间:2022-08-08 19:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:玩转spring boot 快速开始(1)
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9   [b]一、创建一个spring boot的mcv web应用程序[/b] 打开Eclipse,新建Maven项目 [img]http://files.jb51.net/file_images/article/201701/201701040919521.png[/img] 选择quickstart模板 [img]http://files.jb51.net/file_images/article/201701/201701040919522.png[/img] 完成Maven项目的创建 [img]http://files.jb51.net/file_images/article/201701/201701040919523.png[/img] 参照spring的官方例子:[url=http://spring.io/guides/gs/testing-web/]http://spring.io/guides/gs/testing-web/[/url] 在pom.xml增加maven依赖
<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.github.carter659</groupId>
 <artifactId>spring01</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>spring01</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
 </parent>

 <dependencies>
  <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>

添加一个控制器文件“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应用";
 }

}

[img]http://files.jb51.net/file_images/article/201701/201701040919526.png[/img] 修改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程序 [img]http://files.jb51.net/file_images/article/201701/201701040919527.png[/img] 当控制台处显示运行结果后 [img]http://files.jb51.net/file_images/article/201701/201701040919528.png[/img] 在浏览器输入“http://localhost:8080/”网站访问第一个spring boot的应用 [img]http://files.jb51.net/file_images/article/201701/201701040919529.png[/img] [b]二、我们如何对spring boot做单元测试?[/b] pom.xml中增加单元测试的依赖
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
</dependency>

在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(this.restTemplate.getForObject("http://localhost:" + port + "/",
    String.class)).contains("你好,这是第一个spring boot应用");
 }
}

[img]http://files.jb51.net/file_images/article/201701/2017010409195210.png[/img] 并运行单元测试 [img]http://files.jb51.net/file_images/article/201701/2017010409195211.png[/img] 出现绿色表示断言成功 [img]http://files.jb51.net/file_images/article/201701/2017010409195312.png[/img] [b]三、我们怎么部署spring boot?[/b] 我们会按照以下这几个步骤: 1.下载maven 在maven的官方网站下载maven的bin包:http://maven.apache.org/download.cgi [img]http://files.jb51.net/file_images/article/201701/2017010409195313.png[/img] 2.配置环境变量: 我这里是把maven解压到d盘的Program Files (x86)目录 [img]http://files.jb51.net/file_images/article/201701/2017010409195314.png[/img] [img]http://files.jb51.net/file_images/article/201701/2017010409195315.png[/img] 在环境变量中输入:MAVEN_HOME -->D:\Program Files (x86)\maven [img]http://files.jb51.net/file_images/article/201701/2017010409195316.png[/img] Path中追加:;%MAVEN_HOME%\bin; 在CDM窗口中输入“mvn -v”命令来测试maven是否安装成功 [img]http://files.jb51.net/file_images/article/201701/2017010409195317.png[/img] 这里显示的是3.3.9版本 3.打包 在程序所在目录(与pom.xml同级)输入“mvn package”命令: [img]http://files.jb51.net/file_images/article/201701/2017010409195318.png[/img] 出现“BUILD SUCCESS”则表示打包成功 [img]http://files.jb51.net/file_images/article/201701/2017010409195319.png[/img] 会在tatget目录下出现打包后的jar文件 [img]http://files.jb51.net/file_images/article/201701/2017010409195320.png[/img] 4.运行 在CMD中输入命令“java -jar target/spring01-0.0.1-SNAPSHOT.jar” [img]http://files.jb51.net/file_images/article/201701/2017010409195321.png[/img] 这时程序就部署好了,是不是发现spring boot程序不仅开发和测试非常简单,部署也非常容易? 代码下载:[url=https://github.com/carter659/spring-boot-01.git]https://github.com/carter659/spring-boot-01.git[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部