Java 400使用Spring Boot时的错误请求
我正在使用Spring Boot向我的RESTful API构建发送一个Http POST请求,并得到“400错误请求”响应 我的邮寄请求是由邮递员提出的,发送至Java 400使用Spring Boot时的错误请求,java,spring,spring-boot,rest,http,Java,Spring,Spring Boot,Rest,Http,我正在使用Spring Boot向我的RESTful API构建发送一个Http POST请求,并得到“400错误请求”响应 我的邮寄请求是由邮递员提出的,发送至 http://localhost:8080/executebash 用身体 { "filename": "blaba" } 我想把filename变量传递给我的Java方法。 我的RESTful api是用Java和Spring Boot构建的 @RestController pub
http://localhost:8080/executebash
用身体
{
"filename": "blaba"
}
我想把filename
变量传递给我的Java方法。
我的RESTful api是用Java和Spring Boot构建的
@RestController
public class PrapiController {
private Process process;
@RequestMapping(value = "/executebash", produces ="application/json", method = RequestMethod.POST)
public String executeBashScript(@RequestParam String filename) {
//...
}
}
我在@RequestMapping
注释中尝试了使用和不使用输出。
我不知道错误是从哪里来的,也许你能帮我
问候 对于注释,您必须在请求URL中使用param,而不是带有JSON的正文:
http://localhost:8080/executebash?filename=blaba
如果您想使用JSON,您必须使用with data TRANSPORT object或
Map
,如@pcsutar所说。使用@RequestBody
接受请求正文中的数据。如下例所示:
@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 "{}";
}
}
@RestController
公共类PrapiController{
私有过程;
@RequestMapping(value=“/executebash”,consumes=“application/json”,products=“application/json”,method=RequestMethod.POST)
公共字符串executeBashScript(@RequestBody映射输入){
字符串文件名=input.get(“文件名”);
返回“{}”;
}
}
我实现了这一点,但现在得到了响应:415不支持的媒体类型。我需要更改/添加什么?需要在RequestMapping中添加consumes=“application/json”
。或者您可以使用@PostMapping(“/executebash”)
注释。我已经更新了答案。