Rest 来自post man和curl的URL编码数据的Spring Boot

Rest 来自post man和curl的URL编码数据的Spring Boot,rest,post,Rest,Post,如何在SpringBoot中处理这样的curl/postman请求 curl -X POST -H "Accept: application/json" -H "Authorization: Basic mytoken" -d '{"searchText":["aaa","bbb", "ccc", "ddd", "eee","fff"]}' "http://localhost:8080/api/loc 这是我的控制器代码 @RequestMapping(value = "/search", m

如何在SpringBoot中处理这样的curl/postman请求

curl -X POST -H "Accept: application/json" -H "Authorization: Basic mytoken" -d '{"searchText":["aaa","bbb", "ccc", "ddd", "eee","fff"]}' "http://localhost:8080/api/loc
这是我的控制器代码

@RequestMapping(value = "/search", method = RequestMethod.POST, headers="Accept=application/json", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String getWordCountList(@ModelAttribute WordCount searchText) {  
}

注意,WordCount是模型类,它包含两个字符串。但是,当发送上述curl请求时,searchText始终为空。。有什么建议吗?

尝试将@RequestMapping值从“/search”更改为“/api/loc”

另外,请尝试以下模板。剩下的那一种风格:

@RestController
public class MyController {

    @RequestMapping(value = "/api/loc", method = RequestMethod.POST)
    public String getWordCountList(@RequestBody WordCount searchText) {
       return "hello";
    }

}
@RestController
是一个方便的注释,它强调了控制器的REST完整特性,并添加了@ResponseBody语义
@RequestMapping
方法返回类型。您可以使用
@Controller
注释重写示例:

@Controller
public class MyController {

    @RequestMapping(value = "/api/loc", method = RequestMethod.POST)
    public @ResponseBody String getWordCountList(@RequestBody WordCount searchText) {
       return "hello";
    }

}

非常感谢。这是一个输入错误,/api/doc或/search是无关紧要的,其中还有一些内容。问题仍然存在。这里的另一点是,-d和curl命令中给出的值,如果导入到邮递员,数据将单独进入x-www-form-urlencoded部分键,而不是进入值部分