Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 400使用Spring Boot时的错误请求_Java_Spring_Spring Boot_Rest_Http - Fatal编程技术网

Java 400使用Spring Boot时的错误请求

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

我正在使用Spring Boot向我的RESTful API构建发送一个Http POST请求,并得到“400错误请求”响应

我的邮寄请求是由邮递员提出的,发送至

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”)
注释。我已经更新了答案。