SpringMVC學習(請求與響應。常見參數類型接收與響應。@RequestParam、@RequestBody的使用)(詳細示例)

目錄

一、請求與響應。(@RequestMapping)

(1)使用注解@RequestMapping對業務模塊區分。

StudentController。

TeacherController。

(2)Apifox請求與響應。

"/student/login"。

"/teacher/login"。

二、常見參數類型接收。

(1)字符串與數字類型。

請求路徑參數名與接收請求方法參數名一致。

請求路徑參數名與接收請求方法參數名不一致。

注解@RequestParam(name="?")。

(2)接收請求方法參數為實體對象。

(3)請求路徑傳參的實體對象中還有實體對象。

實體類。

代碼示例。

請求與響應。

(4)接收數組類型的參數。

代碼示例。

請求與響應。

(5)接收集合類型的參數。

請求數據當成普通數據放入集合。(注解@RequestParam)

請求數據當成對象類型放入集合。(注解@RequestBody)

(6)接收JSON格式的參數。

<1>核心依賴(jackson)。

<2>SpringMVC配置類。

<3>JSON數據集合對象接收。(@RequestBody)

<4>JSON數據實體對象接收。(@RequestBody)

<5>JSON數據集合接收,但集合內存儲實體對象。(@RequestBody)

(7)日期類型參數傳遞。

<1>請求日期參數類型:年/月/日。

<2>請求日期參數類型:年-月-日。

<3>請求日期參數類型:年-月-日 時-分-秒。

<4>代碼綜合示例。

<5>請求與響應。


一、請求與響應。(@RequestMapping)

(1)使用注解@RequestMapping對業務模塊區分。
  • StudentController。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import com.hyl.service.StudentService;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;@RestController
@RequestMapping("/student")
public class StudentController {@Resourceprivate StudentService studentService;@PostMapping("/login")public Result login(@RequestBody Student student){System.out.println("StudentController login,,,");Student dbStudent = studentService.login(student);System.out.println("login success,,,");System.out.println(dbStudent);return Result.success(dbStudent);}}
  • TeacherController。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import com.hyl.pojo.Teacher;
import com.hyl.service.TeacherService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping("/teacher")
public class TeacherController {@Resourceprivate TeacherService teacherService;@PostMapping("/login")public Result login(@RequestBody Teacher teacher){System.out.println("TeacherController login,,,");Teacher dbTeacher = teacherService.login(teacher);System.out.println("login success,,,");System.out.println(dbTeacher);return Result.success(dbTeacher);}
}

(2)Apifox請求與響應。
  • "/student/login"。

  • "/teacher/login"。

二、常見參數類型接收。

(1)字符串與數字類型。
  • 請求路徑參數名與接收請求方法參數名一致。
  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(String username, String password) {System.out.println("TestController,,,");System.out.println("username:" + username);System.out.println("password:" + password);return Result.success();}
}
  • 請求與響應。


  • 請求路徑參數名與接收請求方法參數名不一致。



  • 注解@RequestParam(name="?")。
  • 解決請求參數名與接收方法參數名不一致問題
package com.hyl.controller;import com.hyl.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(@RequestParam(name = "username111")String username, String password) {System.out.println("TestController,,,");System.out.println("username:" + username);System.out.println("password:" + password);return Result.success();}
}
(2)接收請求方法參數為實體對象。
  • 請求參數名要與實體類屬性名必須一致
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(Student student) {System.out.println("TestController,,,");System.out.println("username:" + student.getUsername());System.out.println("password:" + student.getPassword());return Result.success(student);}
}
  • 請求與響應。


(3)請求路徑傳參的實體對象中還有實體對象。
  • 實體類。
package com.hyl.pojo;import lombok.Data;@Data
public class City {private String province;  //省private String county;  //縣
}
package com.hyl.pojo;import lombok.Data;@Data
public class Student {private Integer id;private String username;private String password;private String name;private City city;  //歸屬地
}

  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(Student student) {System.out.println("TestController,,,");System.out.println("username:" + student.getUsername());System.out.println("password:" + student.getPassword());System.out.println("省份:" + student.getCity().getProvince());System.out.println("縣區:" + student.getCity().getCounty());return Result.success(student);}
}

  • 請求與響應。


(4)接收數組類型的參數。
  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(String[] sports) {System.out.println("TestController,,,");for (String s : sports) {System.out.println("運動:"+s);}return Result.success(sports);}
}

  • 請求與響應。


(5)接收集合類型的參數。
  • 請求數據當成普通數據放入集合。(注解@RequestParam)
package com.hyl.controller;import com.hyl.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/demo")public Result demo(@RequestParam List<String> sports) {System.out.println("TestController,,,");for (String sport : sports) {System.out.println(sport);}return Result.success(sports);}
}


  • 請求數據當成對象類型放入集合。(注解@RequestBody)
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/test")
public class TestController {@PostMapping("/demo")public Result demo(@RequestBody List<Student> students) {System.out.println("TestController,,,");for (Student student : students) {System.out.println(student);}return Result.success(students);}
}


(6)接收JSON格式的參數。
<1>核心依賴(jackson)。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.14.2</version></dependency>

<2>SpringMVC配置類。
package com.hyl.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration
@ComponentScan("com.hyl.controller")
@EnableWebMvc  //開啟JSON接收與轉換
public class SpringMvcConfig {
}

<3>JSON數據集合對象接收。(@RequestBody)
  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/test")
public class TestController {@PostMapping("/demo")public Result demo(@RequestBody List<String> sports) {System.out.println("TestController,,,");for (String sport : sports) {System.out.println(sport);}return Result.success(sports);}
}
  • 請求與響應。


<4>JSON數據實體對象接收。(@RequestBody)
  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/test")
public class TestController {@PostMapping("/demo")public Result demo(@RequestBody Student student) {System.out.println("TestController,,,");System.out.println(student);return Result.success(student);}
}
  • 請求與響應。


<5>JSON數據集合接收,但集合內存儲實體對象。(@RequestBody)
  • 注意若集合存儲非引用類型數據可以使用注解@RequestParam
  • 代碼示例。
package com.hyl.controller;import com.hyl.common.Result;
import com.hyl.pojo.Student;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/test")
public class TestController {@PostMapping("/demo")public Result demo(@RequestBody List<Student> students) {System.out.println("TestController,,,");for (Student student : students) {System.out.println(student);}return Result.success(students);}
}
  • 請求與響應



(7)日期類型參數傳遞。
<1>請求日期參數類型:年/月/日。
  • 日期類型格式是"年/月/日"這種時,只需要用Java的Date類對象接收就可以。


<2>請求日期參數類型:年-月-日。
  • 日期類型格式是"年-月-日"這種時,接收參數Date類對象就需要使用注解@DateTimeFormat(pattern = "yyyy-MM-dd")來進行處理才不會報錯。


<3>請求日期參數類型:年-月-日 時-分-秒。
  • 日期類型格式"年-月-日 時-分-秒"。接收參數Date類對象就需要使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")來進行處理才不會報錯。


<4>代碼綜合示例。
package com.hyl.controller;import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Date;@Controller
@RequestMapping("/test")
public class TestController {@GetMapping("/test4")@ResponseBodypublic String test4(Date date,@DateTimeFormat(pattern = "yyyy-MM-dd")Date date2,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date date3,@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss")Date date4){System.out.println("testController test4");System.out.println(date);System.out.println(date2);System.out.println(date3);System.out.println(date4);return "success 200";}}

<5>請求與響應。


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/76080.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/76080.shtml
英文地址,請注明出處:http://en.pswp.cn/web/76080.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

回溯算法+對稱剪枝——從八皇后問題到數獨問題(二)

引入&#xff1a; 本節我們進一步完善八皇后問題&#xff0c;學習剪枝、八皇后殘局問題 進一步領會邏輯編程的概念&#xff0c;深入體會回溯算法&#xff0c;回顧上一節提到的啟發搜索策略。 回顧&#xff1a; 八皇后問題&#xff1a;我們需要在一個空棋盤上放置 n 個皇后&a…

【玩泰山派】MISC(雜項)- 使用vscode遠程連接泰山派進行開發

文章目錄 前言流程1、安裝、啟動sshd2、配置一下允許root登錄3、vscode中配置1、安裝remote插件2、登錄 **注意** 前言 有時候要在開發板中寫一寫代碼&#xff0c;直接在終端中使用vim這種工具有時候也不是很方便。這里準備使用vscode去通過ssh遠程連接泰山派去操作&#xff0…

【VsCode】設置文件自動保存

目錄 一、前言 二、操作步驟 一、前言 VSCode中開啟自動保存功能可以通過訪問設置、修改settings.json文件、使用自動保存延遲功能來實現。這些方法能有效提升編程效率、避免數據丟失、實時同步更改。 二、操作步驟 在 Visual Studio Code (VS Code) 中設置自動保存功能非…

Adobe After Effects的插件--------Optical Flares之Options概述

Optical Flares插件的Options是對整個效果的組裝和設置。點擊該按鈕會彈出一個組裝室彈窗。 Options組裝室就是對每個【鏡頭對象】進行加工處理,再將其組裝在一起,拼湊成完整的光效。 接下來是我對組裝室的探索: 面板 面板中有預覽、堆棧、編輯和瀏覽按鈕,其作用是調節窗…

如何用 esProc 補充數據庫 SQL 的缺失能力

某些數據庫 SQL 缺失必要的能力&#xff0c;通常要編寫大段的代碼&#xff0c;才能間接實現類似的功能&#xff0c;有些情況甚至要改用存儲過程&#xff0c;連結構都變了。常見的比如&#xff1a;生成時間序列、保持分組子集、動態行列轉換、自然序號、相對位置、按序列和集合生…

迷你世界腳本腳本常見問題

腳本常見問題 彼得兔 更新時間: 2024-05-22 17:54:44 在查閱開發者學院中的腳本API時&#xff0c;若有任何問題或建議&#xff0c;歡迎通過問卷進行反饋&#xff01;【點我填寫問卷】 1.Block中的data在什么地方使用 data使用有具體需求,此處不建議開發者使用。開發者盡可能使…

四、Appium Inspector

一、介紹 Appium Inspector 是一個用于移動應用自動化測試的圖形化工具&#xff0c;主要用于檢查和交互應用的 UI 元素&#xff0c;幫助生成和調試自動化測試腳本。類似于瀏覽器的F12(開發者工具),Appium Inspector 的主要作用包括&#xff1a;? 1.?檢查 UI 元素? …

android11通過白名單卸載安裝應用

目錄 1.源碼路徑: 2.準備文件package.conf: 3.安裝方法installPackagesLI 4.卸載方法deletePackageX 1.源碼路徑: frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java public static final String WHITELIST_PATH="/data/misc/pa…

qt mapFrom返回的QPoint和event->pos()區別和globalPos區別

mousePressEvent 和 eventFilter 里 event.pos 不一樣&#xff0c;一定要注意 eventFilter里event.pos 直接返回相對于label左上角的坐標&#xff0c;就不要再mapFrom mousePressEvent 里event.pos 返回是相對于窗口左上角的坐標&#xff0c;需要用mapFrom返回label左上角的…

Hadoop四 Hive語法

一 數據庫操作 Hive數據庫操作&#xff0c;與MySql有很多都是一致的 創建數據庫 create database if not exists myhive; use myhive;查看數據庫詳細信息 desc database myhive;數據庫本質上就是在HDFS之上的文件夾&#xff0c;是一個以.db結尾的目錄&#xff0c;默認存…

前端VUE框架理論與應用(10)

1、記住全局注冊的行為必須在根 Vue 實例 (通過 new Vue) 創建之前發生。 2、要注意,以 / 開頭的嵌套路徑會被當作根路徑。 這讓你充分的使用嵌套組件而無須設置嵌套的路徑。 3、注意:在 Vue 實例內部,你可以通過 $router 訪問路由實例。因此你可以調用 this.$router.push…

leetcode-單調棧26

關于單調棧的順序總結&#xff1a; 尋找右邊第一個比我大的&#xff1a;從左到右遍歷&#xff0c;棧單調遞減 尋找左邊第一個比我小的&#xff1a;從左到右遍歷&#xff0c;棧單調遞增 尋找右邊第一個比我小的&#xff1a;從右到左遍歷&#xff0c;棧單調遞增 尋找左邊第一個比…

Linux:安裝 CentOS 7(完整教程)

文章目錄 一、簡介二、安裝 CentOS 72.1 虛擬機配置2.2 安裝CentOS 7 三、連接遠程服務器&#xff08;擴展&#xff09;3.1 獲取虛擬機 IP 地址3.2 連接遠程服務器 四、結語 一、簡介 CentOS&#xff08;Community ENTerprise Operating System&#xff09;是一個基于 Linux 的…

Nautilus 正式發布:為 Sui 帶來可驗證的鏈下隱私計算

作為 Sui 安全工具包中的強大新成員&#xff0c;Nautilus 現已上線 Sui 測試網。它專為 Web3 開發者打造&#xff0c;支持保密且可驗證的鏈下計算。Nautilus 應用運行于開發者自主管理的可信執行環境&#xff08;Trusted Execution Environment&#xff0c;TEE&#xff09;中&a…

Git完全指南:從入門到精通版本控制 ------- Git 工作流程 (3)

Git工作流程完全指南&#xff1a;從入門到高效協作 引言 Git作為分布式版本控制系統的行業標準&#xff0c;其高效的分支管理能力是團隊協作的基石。本文將深入解析標準Git工作流程&#xff0c;助你掌握從代碼提交到團隊協作的全鏈路實踐。 一、Git核心概念速覽 三大工作區域 …

Distortion, Animation Raymarching

這節課的主要目的是對uv進行操作&#xff0c;實現一些動畫的效果&#xff0c;實際就是采樣的動畫 struct texDistort {float2 texScale(float2 uv, float2 scale){float2 texScale (uv - 0.5) * scale 0.5;return texScale;}float2 texRotate(float2 uv, float angle){float…

《vue3學習手記3》

標簽的ref屬性 vue3和vue2中的ref屬性&#xff1a; 用在普通DOM標簽上&#xff0c;獲取的是DOM節點 ref用在組件標簽上&#xff0c;獲取的是組件實例對象 區別在于&#xff1a; 1.vue3中person子組件中的數據父組件App不能直接使用&#xff0c;需要引入并使用defineExpose才可…

List基礎與難度題

1. 向 ArrayList 中添加元素并打印 功能描述&#xff1a; 程序創建一個空的 ArrayList 集合&#xff0c;用于存儲字符串類型的元素。向該 ArrayList 中依次添加指定的字符串元素。使用增強型 for 循環遍歷 ArrayList 中的所有元素&#xff0c;并將每個元素打印輸出到控制臺。 …

樓宇自控系統如何為現代建筑打造安全、舒適、節能方案

在科技飛速發展的當下&#xff0c;現代建筑對功能和品質的要求日益提升。樓宇自控系統作為建筑智能化的核心技術&#xff0c;宛如一位智慧的“管家”&#xff0c;憑借先進的技術手段&#xff0c;為現代建筑精心打造安全、舒適、節能的全方位解決方案&#xff0c;讓建筑真正成為…

綠算輕舟系列FPGA加速卡:驅動數字化轉型的核心動力【2】

工業與醫療&#xff1a;精準化的幕后推手 在工業4.0與智慧醫療領域&#xff0c;綠算輕舟FPGA加速卡通過實時信號處理與高精度控制&#xff0c;推動關鍵場景的技術升級。 工業自動化&#xff1a;在機器視覺質檢中&#xff0c;實現亞像素級缺陷檢測&#xff0c;產線檢測速度大幅…