Spring Rest Controller接收Ajax JSON数组POST请求报404错误
排查Spring Rest Controller POST请求404错误及解决方案
我来帮你梳理这个问题的核心原因,以及对应的解决办法:
一、核心问题分析
你的请求返回404,本质上是请求路径不匹配+参数接收方式不兼容共同导致的,具体来说:
- 参数接收逻辑错误:前端发送的是JSON格式的请求体(通过
JSON.stringify处理并指定contentType: "application/json"),但后端用@RequestParam来接收参数——@RequestParam是从URL查询参数(比如?ids=a&ids=b&name=23)或表单数据(application/x-www-form-urlencoded)中取值,完全无法识别JSON请求体里的参数,Spring找不到能匹配当前请求的处理方法,就会返回404。 - 请求路径可能存在相对路径问题:前端的
url: "api/testEnvironment/update"是相对路径,如果当前页面的URL层级和API路径不匹配,就会导致请求发送到错误的地址,触发404。
二、具体解决方案
1. 修正后端参数接收方式
你需要用@RequestBody来接收JSON请求体,有两种实现方式:
方式一:创建DTO类(推荐,更规范)
先定义一个用于接收请求参数的DTO类:
public class TestEnvironmentUpdateDTO { private String[] ids; private String name; // 必须提供Getter和Setter,Spring才能自动映射JSON字段 public String[] getIds() { return ids; } public void setIds(String[] ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
然后修改Controller方法,用@RequestBody接收这个DTO:
import org.springframework.util.Arrays; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestEnvironmentController { @RequestMapping(value = "/api/testEnvironment/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> updateEnvironment(@RequestBody TestEnvironmentUpdateDTO dto) { // 打印测试参数 System.out.println(Arrays.toString(dto.getIds())); System.out.println(dto.getName()); return new ResponseEntity<>(HttpStatus.OK); } }
方式二:用Map接收(快速测试用)
如果不想创建DTO类,可以直接用Map<String, Object>来接收请求体:
import org.springframework.util.Arrays; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class TestEnvironmentController { @RequestMapping(value = "/api/testEnvironment/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> updateEnvironment(@RequestBody Map<String, Object> requestBody) { String[] ids = ((List<String>) requestBody.get("ids")).toArray(new String[0]); String name = (String) requestBody.get("name"); System.out.println(Arrays.toString(ids)); System.out.println(name); return new ResponseEntity<>(HttpStatus.OK); } }
2. 修正前端请求路径
把前端的URL改为绝对路径(开头加/),避免相对路径导致的地址错误:
function testing(){ var a = [] ; a.push('a'); a.push('b'); var b = '23'; var search = { "ids" : a, "name" : b } $.ajax({ type : "POST", url : "/api/testEnvironment/update", // 开头加/,确保是根路径下的API contentType: "application/json", data: JSON.stringify(search), // traditional参数是针对表单数组的,JSON请求不需要,可移除 success : function(data) { console.log("请求成功"); }, error : function(e) { console.log("请求失败", e); } }); }
3. 额外注意事项
- 如果你的Spring应用部署时有上下文路径(比如Tomcat中项目名为
myapp),前端URL需要加上上下文路径,比如"/myapp/api/testEnvironment/update"。 - 确保你的Controller类上标注了
@RestController或@Controller+@ResponseBody,否则Spring不会把它识别为REST接口。
内容的提问来源于stack exchange,提问作者Anubhav Kumar
相关产品推荐
相关产品推荐

