[英]400 Bad Request when using Spring Boot
I am sending a Http POST request to my RESTful API build with Spring Boot and get the "400 Bad Request" response.我正在使用 Spring 引导向我的 RESTful API 构建发送 Http POST 请求并获得“400 Bad Request”响应。
My POST request is made with Postman, send to我的 POST 请求是用 Postman 发出的,发送到
http://localhost:8080/executebash
with the body与身体
{
"filename": "blaba"
}
I want to pass the filename
variable to my Java Method.我想将
filename
变量传递给我的 Java 方法。 My RESTful api is build in Java with Spring Boot我的 RESTful api 是在 Java 中构建的,带有 Spring 引导
@RestController
public class PrapiController {
private Process process;
@RequestMapping(value = "/executebash", produces ="application/json", method = RequestMethod.POST)
public String executeBashScript(@RequestParam String filename) {
//...
}
}
I tried with and with out produces in the @RequestMapping
annotation.我尝试在
@RequestMapping
注释中使用和不使用产生。 I have no idea where the error comes from, maybe you can help me.我不知道错误来自哪里,也许你可以帮助我。
Regards!问候!
Use @RequestBody
to accept data in request body.使用
@RequestBody
接受请求正文中的数据。 As shown in below example:如下例所示:
@RestController
public class PrapiController {
private Process process;
@RequestMapping(value = "/executebash", consumes="application/json", produces ="application/json", method = RequestMethod.POST)
public String executeBashScript(@RequestBody Map<String, String> input) {
String filename = input.get("filename");
return "{}";
}
}
With @RequestParam
annotation you have to use param in request URL instead of body with JSON:使用
@RequestParam
注释,您必须在请求 URL 中使用 param 而不是带有 JSON 的正文:
http://localhost:8080/executebash?filename=blaba
If you want use your JSON, you have to use @RequestBody
with data transfer object or Map<String, String>
like @pcsutar said.如果你想使用你的 JSON,你必须使用
@RequestBody
和数据传输 object 或Map<String, String>
就像@pcsutar 说的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.