1、引用springboot框架
- 1、在maven項目底下的pom.xml的
中,引用springboot,如下
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!--項目父標簽中引用springboot框架-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--artifactId是項目的默認值,每個module名可在后續的module標簽中定義-->
<groupId>com.mycom.apitest</groupId>
<artifactId>exercise</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>exercise10</module>
<module>exercise11</module>
<module>exercise12</module>
<module>exercise13</module>
</modules>
<!--指定build的Java版本-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 2、在module的pom.xml中引入springboot依賴,如下
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>exercise</artifactId>
<groupId>com.mycom.apitest</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>exercise10</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、運行官網demo
* 新建包hello,並在包下新建測試類SampleController.java,其中內容為官網demo的代碼,如下
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
//請求目錄
@RequestMapping("/") //訪問路徑為根目錄,即域名加端口即可訪問
@ResponseBody //響應體,home為響應主頁
String home() {
return "Hello World!";
}
//spring應用運行SampleController類
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
-
啟用執行結果為:
springboot 內嵌的是tomcat 服務,默認的端口是8080,若端口被占用的話,會啟動失敗。 -
瀏覽器訪問應用如下:
3、返回cookies信息的get接口開發
- 1、在main/java下新建一個入口類:Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication //加這個注解標簽,表示將下面的入口類托管
@ComponentScan("com.course.server") //表示托管給我后,你要我掃描哪個包下的類
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args); //固定寫法,傳入參數args與方法中的args相同
}
}
-
2、在main/java下新建托管后需要掃描的包com.course.server,並建需要掃描的測試類MyGetMethod.java,如下
-
無cookie信息返回的寫法,MyGetMethod.java
package com.course.server;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController //表示以下類是需要被掃描的
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET) //請求映射地址和請求方法,即請求路徑,可與后面的方法名相同
public String getCookies(){
return "恭喜你獲取cookies信息成功"; //響應信息
}
}
- 有cookie信息返回的寫法,MyGetMethod.java
package com.course.server;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController //表示以下類是需要被掃描的
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET) //請求映射地址和請求方法,即請求路徑,可與后面的方法名相同
public String getCookies(HttpServletResponse response){
//HttpServletRequest 裝載請求信息的類
//HttpServletResponse 裝載響應信息的類
//定義響應的cookie信息
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie); //將cookie信息添加到響應中返回
return "恭喜你獲取cookies信息成功"; //響應信息
}
}
- 3、在resources下,新建一個配置文件,名字必須為
application.properties
,取這個名字,springboot框架才會將這個配置文件自動加載。且里面的字段內容為固定寫法,后面具體的值可自定義,比如端口號,這里修改springboot啟動的端口為8888,如下:
server.port=${port:8888}
- 4、執行后,訪問結果為:
4、一個要求攜帶cookies信息訪問的get接口開發
- 修改MyGetMethod.java部分代碼如下
package com.course.server;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
@RestController //表示以下類是需要被掃描的
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
public String getCookies(HttpServletResponse response){
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你獲取cookies信息成功";
}
/*
* 要求客戶端攜帶cookies訪問
* 這是一個需要攜帶cookies信息才能訪問的get請求
* */
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
public String getWithCookies(HttpServletRequest request){
//獲取請求中的cookie信息,並存在一維數組中,因可能有多個cookie信息
Cookie[] cookies = request.getCookies();
//定義cookie信息為空時的響應信息
if (Objects.isNull(cookies)){
return "你必須攜帶cookies信息來!";
}
//將獲取到的cookie信息遍歷出來比對,比對通過則返回成功相應信息
for (Cookie cookie : cookies){
if (cookie.getName().equals("login") &&
cookie.getValue().equals("true")){
return "成功,這是一個需要攜帶cookies信息才能訪問的get請求!";
}
}
//如果獲取到的cookie信息比對不正確,則返回以下信息(即除了上面兩種情況,剩下的情況都返回以下信息)
return "你必須攜帶正確的cookies信息來!!!";
}
}
5、需攜帶參數的get請求的兩種開發方式
第一種實現方式(參數在問號后面)
- 1、如下是對參數不做校驗,傳入什么參數都受理,包括空參數,也返回正確的響應(修改MyGetMethod.java部分代碼)
package com.course.server;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@RestController
public class MyGetMethod {
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
//將參數定義在方法傳參位置處,用@RequestParam關鍵字,如下,需要傳兩個參數
//泛數據類型(對象類型)
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
//泛數據類型的類,在實例化對象時,具體化元素的數據類型
//響應體,如下返回的是json格式的信息
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("襯衫",300);
return myList;
}
}
-
不管傳入什么參數值,都會返回正確結果,這種方式可以用於獲取傳入的參數。結果為:
-
2、如下是對參數做校驗,傳入正確的參數,返回正確的信息,否則返回錯誤信息(修改MyGetMethod.java部分代碼)
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
//required=true:該參數不能為空;相反required=false:該參數能為空;若不寫,則默認為true
public Map<String,Integer> getList(@RequestParam(value = "start", required = false) Integer start,
@RequestParam(value = "end", required = false) Integer end){
//響應體,如下返回的是json格式的信息
Map<String, Integer> myList = new HashMap<>();
if (start.equals(15) && end.equals(30)) {
myList.put("鞋", 400);
myList.put("干脆面", 1);
myList.put("襯衫", 300);
return myList;
}
//參數錯誤,則返回以下信息,空值則返回500
myList.put("oh sorry start or end is wrong", 0);
return myList;
}
第二種實現方式(參數在路徑中)(修改MyGetMethod.java部分代碼)
/**
*第2種需要攜帶參數訪問的get請求,用到的是@PathVariable 關鍵字,因為是參數化的路徑,有校驗
* url: ip:port/get/with/param/10/20
* */
@RequestMapping(value = "/get/with/param/{start}/{end}") //另一種請求url
public Map myGetList(@PathVariable Integer start,
@PathVariable Integer end){
Map<String, Integer> myList = new HashMap<>();
if (start.equals(15) && end.equals(30)) {
myList.put("鞋", 400);
myList.put("干脆面", 1);
myList.put("襯衫", 300);
return myList;
}
//參數錯誤,則返回以下信息,空值則返回500
myList.put("oh sorry start or end is wrong", 0);
return myList;
}
//不做參數校驗
@RequestMapping(value = "/get/with/param2/{start}/{end}")
public Map myGetList2(@PathVariable Integer start,
@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("襯衫",300);
myList.put("干脆面",1);
return myList;
}