如何在spring boot
中处理这样的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始终为空..有什么建议吗?
答案 0 :(得分:0)
尝试更改" / search"中的@RequestMapping值到" / api / loc"。
另外,请尝试以下模板。 REST-ful风格的那个:
@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";
}
}