spring mvc controller POST不接受卷曲

时间:2017-02-15 04:14:50

标签: spring-mvc spring-boot

我有一个Spring boot 1.5应用程序,我正在尝试正常工作。这是我试图支持的POST卷曲:

  

curl -i -X POST -d' {" words":[" read","亲爱的","敢于&# 34;]}' http://localhost:3000/words.json -H" Content-Type:application / json"

我的终点是:

@RestController
@RequestMapping("/words")
public class WordsController {

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity addWordsToCorpus(@RequestBody List<String> wordsToAdd) {
...
}

但是,这些值没有使用此错误进入方法体:

{"timestamp":1487131782829,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@51010449; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@51010449; line: 1, column: 1]","path":"/words.json"}

我有杰克逊在路上,我认为我的卷曲是基于jsonformatter网站提交有效的JSON。

1 个答案:

答案 0 :(得分:2)

请使用:

curl -i -X POST -d '["read", "dear", "dare"]' http://localhost:3000/words.json -H "Content-Type: application/json"

或者如果您想使用words来引用["read", "dear", "dare"],您应该将类​​定义为方法参数,如下所示:

public class Word {

    private List<String> words;

    setter and getter
}

和控制器应该是

@RestController
@RequestMapping("/words")
public class WordsController {

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity addWordsToCorpus(@RequestBody Word word) {
...
}