向rest API获取方法添加参数(ReactJS>;Spring boot)
我只是想用一个fetch方法向我的restapi发送一个值,但似乎无法让它工作。 这就是我目前拥有的: 反应:向rest API获取方法添加参数(ReactJS>;Spring boot),reactjs,spring-boot,post,fetch,restapi,Reactjs,Spring Boot,Post,Fetch,Restapi,我只是想用一个fetch方法向我的restapi发送一个值,但似乎无法让它工作。 这就是我目前拥有的: 反应: getFloorplans() { fetch('/api/ssid') .then(response => response.json()) .then(message => { this.setState({ floorplan: me
getFloorplans() {
fetch('/api/ssid')
.then(response => response.json())
.then(message => {
this.setState({
floorplan: message[0]
})
});
fetch('/api/ssid', {
method: "POST",
//make sure to serialize your JSON body
value: {ssid: this.state.SSID}
})
.then( (response => response.json()
.then(message => {
console.log(message);
})
))};
Spring boot rest api:
@RestController
public class FloorplanController {
//Floorplan floorplan = new Floorplan();
@RequestMapping("/api/ssid")
public void someMethod(@RequestParam String value) {
System.out.println(value);
}
}
这是我当前遇到的错误:
异常是告诉您缺少请求参数。这意味着您的控制器中缺少
@RequestParam String value
的值。请求应该如下所示http://yourhost/api/ssid?value=somevalue
您应该在正文中设置标题和参数
fetch('/api/ssid'{
方法:“张贴”,
标题:{
“内容类型”:“应用程序/json”
},
正文:{ssid:this.state.ssid}
})
您应该使用@PostMapping(“/api/ssid”)
如果您正在执行POST请求,目前没有指定HTTP方法。第二件事,我不知道有多少反应,但如果您将值作为JSON主体发送,则应该在方法中使用@RequestBody
。此外,我认为您必须使用主体
,而不是值
,这就是fetch
所期望的:那么我该如何从控制器中的主体中获取数据呢?我不熟悉Spring,但您应该有一些请求参数来提取主体@dylan75。这只确认您发送的数据,但我想问题出在我的控制器中,但我不知道如何提取主体数据请参阅此。