51Testing软件测试论坛
标题:
Spring boot 环境搭建 (一)
[打印本页]
作者:
草帽路飞UU
时间:
2017-6-15 09:52
标题:
Spring boot 环境搭建 (一)
前言
目前我们公司后台使用的是spring,很多单元测试和数据mock也是基于此,为了深入了解特别整理了spring环境搭建
顺便给自己总结一下,方便以后使用
1. 新建maven工程导入spring依赖
我使用的是IDEA,熟悉eclipse的可以使用eclipse
2.导入相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.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-autoconfigure</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</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
复制代码
3. 在项目资源文件夹中新建application.yml
注意:必须是这个名字,spring框架启动会去调用
里面内容可以填写端口和接口地址
# Servlet Container Server settings
server:
port: 8081
context-path: /spring/api
spring:
http:
encoding.charset: UTF-8
encoding.enable: true
encoding.force: true
cors.enable: true
#开启resttemplate
mvc:
throw-exception-if-no-handler-found: false
复制代码
4.新建mian启动类
声明是启动类
<span class="nd">@SpringBootApplication</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Application</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">){</span>
<span class="n">SpringApplication</span><span class="o">.</span><span class="na">run</span><span class="o">(</span><span class="n">Application</span><span class="o">.</span><span class="na">class</span><span class="o">,</span><span class="n">args</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
复制代码
@RestController
public class AppController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public UserInf getData(){
UserInf inf=new UserInf();
inf.setAge(18);
inf.setName("张三");
return inf ;
}
}
复制代码
5. 新建web接口
需要声明为RestController ,指定接口请求方法,接口地址
6. 编译发布
使用maven install和springboot-run自动发布成功
访问接口查看如下,搭建完成
接下来开始数据mock测试吧
作者:
乐哈哈yoyo
时间:
2017-6-15 10:23
学习
作者:
草帽路飞UU
时间:
2017-6-15 10:23
乐哈哈yoyo 发表于 2017-6-15 10:23
学习
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2