最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決axios發(fā)送post請求,springMVC接收不到數據問題的處理

 更新時間:2026年05月20日 10:38:08   作者:svygh123  
文章主要討論了Vue組件無法正確接收和處理Axios請求的問題,并詳細描述了SpringMVC中使用@PathVariable、@RequestBody、@RequestParam的不同場景及其對應的前端Axios寫法

今天發(fā)現一個問題:

vue組件中無法正確接收并處理axios請求

這個問題已經困擾我好久了,在電腦面前坐了兩天只能確定前端應該是正確的發(fā)送了請求,但發(fā)送請求后無法正確接受后端返回的數據。

問題:vue組件無法接受后端數據

錯誤代碼如下:

axios.post("/simple_query",{
},this.simple_query_input).then(res=>{    
    console.log(res);
}).catch(err=>{
    console.log(err);
})
@RequestMapping(value = "/simple_query",method = RequestMethod.POST)
public cheName handleSimpleQuery(@RequestParam("simple_query_input") String simpleQueryInput) throws Exception {

}

網上找到也有類似未解決的:

Spring MVC的Controller里面使用了@RequestParam注解來接收參數,但是只在GET請求的時候才能正常訪問,在使用POST請求的時候會產生找不到參數的異常。原本好好的POST請求開始報400錯誤,找不到REST服務,一般情況下報這種錯誤多是由于POST請求時傳遞的參數不一致,但是這次不存在這種問題,百思不得其解啊。。。

還有這個:axios發(fā)送post請求,springMVC接收不到數據問題

@RequestMapping(method = RequestMethod.POST, value = "/test")
@ResponseBody
public ResponseEntity testDelete(@RequestParam("id") Integer id)
        throws Exception {
    return ResponseEntity.ok();
}

代碼中是規(guī)定了請求方式POST,使用@RequestParam接收id參數。

然后前臺請求參數也對,是這個形式的{id:111},看起來沒錯啊,參數名完全一樣,但是后臺報錯Required String parameter 'id' is not present,說id參數必須傳過來。

分析問題

雖然前端傳遞的參數形式為{id: 111},看起來id的參數名確實是一樣的,但是這個參數是作為請求的body而非作為普通參數或query parameter傳遞的。因此無法直接使用@RequestParam注釋接收它。

今天就倆解決一下吧:

SpringMVC@PathVariable、@RequestBody、@RequestParam的使用場景以及對應的前端axios寫法是什么呢?

一、@PathVariable

axios代碼:

axios.post('http://localhost:8080/endpoint3/' + this.firstName + '/' + this.lastName)
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint3/{firstName}/{lastName}")
@ResponseBody
public String endpoint2(@PathVariable("firstName") String firstName,  
         @PathVariable("lastName") String lastName) {
	// 處理請求邏輯
	return "Hello, " + firstName + " " + lastName;
}

二、@RequestBody

axios代碼:

axios.post('http://localhost:8080/endpoint2', {firstName: this.firstName, lastName: this.lastName})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint2")
@ResponseBody
public String endpoint2(@RequestBody Map<String, Object> requestBody) {
	String firstName = Objects.toString(requestBody.get("firstName"));
	String lastName = Objects.toString(requestBody.get("lastName"));
	// 處理請求邏輯
	return "Hello, " + firstName + " " + lastName;
}

三、@RequestParam

axios代碼:

const formData = new FormData();
formData.append('firstName', this.firstName);
formData.append('lastName', this.lastName);
axios.post('http://localhost:8080/endpoint1', formData)
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint1")
public String handlePostRequest(@RequestParam("firstName") String firstName,
		@RequestParam("lastName") String lastName) {
	// 處理請求邏輯
	return "Hello, " + firstName + " " + lastName;
}

前臺 完整代碼:

<!DOCTYPE html>
<html>
<head>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.0/vue.min.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.6.8/axios.min.js"></script>
</head>
<body>
	<div id="app">
		
	</div>
	<script>
			new Vue({
			  el: '#app',
			  data () {
				return {
				  firstName: "John",
				  lastName: "Doe"
				}
			  },
			  mounted () {
				  const formData = new FormData();
				  formData.append('firstName', this.firstName);
				  formData.append('lastName', this.lastName);
				  axios.post('http://localhost:8080/endpoint1', formData)
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

					axios.post('http://localhost:8080/endpoint2', {firstName: this.firstName, lastName: this.lastName})
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

					axios.post('http://localhost:8080/endpoint3/' + this.firstName + '/' + this.lastName)
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

			  }
			})
	</script>

</body>

后臺核心代碼:

@RestController
@CrossOrigin
public class MySpringMvcController {
	
	@PostMapping("/endpoint1")
	public String handlePostRequest(@RequestParam("firstName") String firstName,
			@RequestParam("lastName") String lastName) {
		// 處理請求邏輯
		return "Hello, " + firstName + " " + lastName;
	}
	
	@PostMapping("/endpoint2")
	@ResponseBody
	public String endpoint2(@RequestBody Map<String, Object> requestBody) {
		String firstName = Objects.toString(requestBody.get("firstName"));
		String lastName = Objects.toString(requestBody.get("lastName"));
		// 處理請求邏輯
		return "Hello, " + firstName + " " + lastName;
	}
	
	@PostMapping("/endpoint3/{firstName}/{lastName}")
	@ResponseBody
	public String endpoint2(@PathVariable("firstName") String firstName,  
	         @PathVariable("lastName") String lastName) {
		// 處理請求邏輯
		return "Hello, " + firstName + " " + lastName;
	}

}

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Spring中配置和讀取多個Properties文件的方式方法

    Spring中配置和讀取多個Properties文件的方式方法

    本篇文章主要介紹了Spring中配置和讀取多個Properties文件的方式方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • java &與&&的區(qū)別及實例

    java &與&&的區(qū)別及實例

    這篇文章主要介紹了java &與&&的區(qū)別的相關資料,并附簡單實例,幫助大家學習理解這部分知識,需要的朋友可以參考下
    2016-10-10
  • Flowable流程引擎API與服務

    Flowable流程引擎API與服務

    這篇文章主要介紹了Flowable流程引擎API與服務,引擎API是與Flowable交互的最常用手段,總入口點是ProcessEngine,使用ProcessEngine,可以獲得各種提供工作流或BPM方法的服務,下面我們來詳細了解
    2023-10-10
  • springboot2中HikariCP連接池的相關配置問題

    springboot2中HikariCP連接池的相關配置問題

    這篇文章主要介紹了springboot2中HikariCP連接池的相關配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java并發(fā)工具類Phaser詳解

    Java并發(fā)工具類Phaser詳解

    這篇文章主要介紹了Java并發(fā)工具類Phaser詳解,Phaser(階段協(xié)同器)是一個Java實現的并發(fā)工具類,用于協(xié)調多個線程的執(zhí)行,它提供了一些方便的方法來管理多個階段的執(zhí)行,可以讓程序員靈活地控制線程的執(zhí)行順序和階段性的執(zhí)行,需要的朋友可以參考下
    2023-11-11
  • Java切面(Aspect)的多種實現方式

    Java切面(Aspect)的多種實現方式

    這篇文章主要給大家介紹了關于Java切面(Aspect)的多種實現方式,在Java開發(fā)中切面(Aspect)是一種常用的編程方式,用于實現橫切關注點(cross-cutting concern),需要的朋友可以參考下
    2023-08-08
  • SpringBoot的自動裝配機制和Starter的實現原理分析

    SpringBoot的自動裝配機制和Starter的實現原理分析

    SpringBoot自動裝配機制通過`@EnableAutoConfiguration`開啟,基于條件化配置實現,簡化開發(fā),Starter作為“開箱即用”的依賴模塊,包含庫依賴、自動配置類和附加配置,自動裝配與Starter結合,用戶只需引入Starter即可獲得完整功能,提升開發(fā)效率
    2026-05-05
  • Java?9中List.of()的使用示例及注意事項

    Java?9中List.of()的使用示例及注意事項

    Java 9引入了一個新的靜態(tài)工廠方法List.of(),用于創(chuàng)建不可變的列表對象,這篇文章主要介紹了Java?9中List.of()的使用示例及注意事項的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-03-03
  • MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    這篇文章主要介紹了MyBatis插入Insert、InsertSelective的區(qū)別及使用心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • MyBatis配置文件元素示例詳解

    MyBatis配置文件元素示例詳解

    在MyBatis框架的核心配置文件中,<configuration>元素是配置文件的根元素,其他元素都要在<contiguration>元素內配置,這篇文章主要介紹了MyBatis配置文件元素,需要的朋友可以參考下
    2023-06-06

最新評論

大丰市| 内丘县| 丹棱县| 和硕县| 思南县| 南澳县| 循化| 遂平县| 红桥区| 淮滨县| 尼木县| 万源市| 板桥市| 乌兰浩特市| 玛曲县| 平顶山市| 托克逊县| 金寨县| 田阳县| 蒙山县| 济源市| 禹城市| 清新县| 阿克陶县| 静安区| 莱州市| 德安县| 洪江市| 延津县| 义马市| 林甸县| 滦南县| 泽州县| 永年县| 安乡县| 镶黄旗| 丰镇市| 沁水县| 桂林市| 鄂尔多斯市| 筠连县|