开发测试框架一 - 创建springboot工程及基础操作

一、创建及运行方式

1. 从官网导入:
注意:由于我的java版本是1.8;所以选中了spring2.7.14;

如果你的java版本是9及以上,选中spring3相关的

同时Java 版本也要对应起来

2. 创建第一个get请求

 创建Controller  package及类,创建以下的代码:

package com.example.aitestmini.controller;

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

//与前端请求的类
@RestController
public class firstController {

    //需求:get请求,路径/first, 前端显示hello springboot
    @GetMapping("/first")
    String first(){
        return "Hello Spring!";
        // http://localhost:8080/first
    }
}

运行:

运行AitestMiniApplication的run()方法

在浏览器访问http://localhost:8080/first,应该是展示hello spring

8080端口被占用,使用以下命令查看占用端口的pid:
lsof -i :8080
执行下面命令kill调进程:
kill -9 pid
3. 打包
mvn package 
执行以上命令,会在target/目录下自动生成jar包
4. 运行方式
方式一:
之前已经说过了,执行run()方法
方式二:
java -jar target/aitest-mini-0.0.1-SNAPSHOT.jar
运行以上命令,jar是刚才打出来的jar包
方式三:
mvn spring-boot:run
执行以上命令

二、端口管理

1. 常见端口实名方式
如果配置文件不声明端口,默认按照8080

1. application.properties配置文件管理端口:
server.port=8081

2. application.yml配置文件管理端口:
server:
  port: 8082

3. 运行命令声明端口
mvn clean package
java -jar -Dserver.port=8083 target/aitest-mini-0.0.1-SNAPSHOT.jar

2. 不同环境配置不同的端口
  • 针对环境创建不同的配置文件

 

 

 3. 运行不同环境端口的方式
1. application.properties管理:

spring.profiles.active=dev

2. application.yml管理
spring:
  profiles:
    active: dev

3. 在pom文件管理
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<profilesActive>dev</profilesActive>
			</properties>
		</profile>

		<profile>
			<id>qa</id>
			<properties>
				<profilesActive>qa</profilesActive>
			</properties>
			<!-- 默认qa环境-->
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
	</profiles>


添加上面的依赖,在application.properties管理:
spring.profiles.active=@profilesActive@

三、Get请求Demo

1. 普通get请求的声明方式
1. 方法名前面使用:@GetMapping("/first")
2. 方法名前面使用:@RequestMapping(path = "/first",method = RequestMethod.GET)
@RestController
public class firstController {

    //需求:get请求,路径/first, 前端显示hello springboot
//    @GetMapping("/first")
    @RequestMapping(path = "/first",method = RequestMethod.GET)
    String first(){
        return "Hello Spring!";
        // http://localhost:8080/first
    }
}
2. 带有参数的Get请求
需求:
http://localhost:8081/topic/{id}
对应浏览器显示地址:{id}为内容
// 如果不想写@PathVariable("id")里面的id,需要保证传入的id->sid
@RestController
public class BaseGetWithIdController {

    @GetMapping("/topic/{id}")
    String getTopic(@PathVariable("id") String tid){
        return "请求的id为 " + tid + " 的内容!!!";
    }
}
3. 在queryParam拼接
需求:http://localhost:8081/native?s={sid}
对应浏览器显示地址:这是一个本国地址为:{sid}的内容
@GetMapping("/native")
    String getNative(@RequestParam("s") String sid){
        return "这是一个本国地址为:" + sid + " 的内容!";
    }

升级:

如果当s=66,打印不一样的内容:


    @GetMapping("/native")
    String getNative(@RequestParam("s") String sid){
        return "这是一个本国地址为:" + sid + " 的内容!";
    }

    @GetMapping(path = "/native", params = {
            "s=66"
    })
    String getNative1(@RequestParam("s") String sid){
        return "这是一个本国地址为:" + sid + " 的内容!getNative1";
    }
4. 在controller前面加@RequestMapping("/t")

效果:相当于全部的链接前面加了/t会被自动识别

代码:

 @RequestMapping(path = "/first",method = RequestMethod.GET)
    String first(){
        return "Hello Spring!";
        // http://localhost:8080/first
    }
5. PathVariable参数非必填
//1. @PathVariable(value = "did",required = false)中的required默认是true,可以设置为false
//2. 声明完成之后, 对应的访问路径会有2中,需在GetMapping里面说明value = {"/topic/{did}/u","/topic/u"}

    @GetMapping(value = {"/topic/{did}/u",
                        "/topic/u"})
    String getTopic1(@PathVariable(value = "did",required = false) String topid,
                     @RequestParam(defaultValue = "66") int sid){
        return "请求的id为 " + topid + " 的内容!!!并且参数sid为" + sid + "的内容!!!";
    }
    //http://localhost:8081/t/topic/99/u
    //http://localhost:8081/t/topic/u
6. RequestParam非必填及提供默认值
    @GetMapping("/top/{city}/{year}")
    String getRUIWithPara(@PathVariable int year,
                          @PathVariable String city,
                          @RequestParam(defaultValue = "GDP",required = false) String desc,
                          @RequestParam(defaultValue = "45666") int money){
        return "{"+ year + "}年{" + city + "}人均{" + desc + "}为:{" + money + "}";
    }
1. desc非必填,但是GetMapping的值只有一个,因为desc的类型不是PathVariable
2. @RequestParam(defaultValue = "GDP",required = false) String desc
    说明RequestParam的required如果为false可以非必填

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值