弹簧启动与post man和curl的urlencoded数据

时间:2017-03-29 06:53:07

标签: rest post

如何在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始终为空..有什么建议吗?

1 个答案:

答案 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";
    }

}