在哪个网站可以做试卷,国外平面设计教程网站,上海环球金融中心简笔画,柳州住房和城乡建设厅网站一、创建及运行方式
1. 从官网导入#xff1a;
注意#xff1a;由于我的java版本是1.8#xff1b;所以选中了spring2.7.14#xff1b;如果你的java版本是9及以上#xff0c;选中spring3相关的同时Java 版本也要对应起来 2. 创建第一个get请求 创建Controller package及…一、创建及运行方式
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 springbootGetMapping(/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. 常见端口实名方式
如果配置文件不声明端口默认按照80801. application.properties配置文件管理端口
server.port80812. application.yml配置文件管理端口
server:port: 80823. 运行命令声明端口
mvn clean package
java -jar -Dserver.port8083 target/aitest-mini-0.0.1-SNAPSHOT.jar 2. 不同环境配置不同的端口
针对环境创建不同的配置文件 3. 运行不同环境端口的方式
1. application.properties管理spring.profiles.activedev2. application.yml管理
spring:profiles:active: dev3. 在pom文件管理profilesprofileiddev/idpropertiesprofilesActivedev/profilesActive/properties/profileprofileidqa/idpropertiesprofilesActiveqa/profilesActive/properties!-- 默认qa环境--activationactiveByDefaulttrue/activeByDefault/activation/profile/profiles添加上面的依赖在application.properties管理
spring.profiles.activeprofilesActive
三、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 的内容!;}
升级
如果当s66打印不一样的内容
GetMapping(/native)String getNative(RequestParam(s) String sid){return 这是一个本国地址为 sid 的内容!;}GetMapping(path /native, params {s66})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可以非必填