http-headers - Curl get json - 如何用cURL POST JSON数据?

Shell curl POST json / json / rest / spring-mvc / curl

我使用Ubuntu并在其上安装了cURL。我想用cURL测试我的Spring REST应用程序。我在Java端编写了POST代码。但是,我想用cURL对其进行测试。我正在尝试发布JSON数据。示例数据如下:

{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}

我用这个命令。

curl -i \
    -H "Accept: application/json" \
    -H "X-HTTP-Method-Override: PUT" \
    -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
    http://localhost:8080/xx/xxx/xxxx

它返回这个错误。

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //考虑@Valid标签
    configuration.setName("PUT worked");
    // todo如果发生错误,则response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return configuration;
}

ivanleoncz



Answer #1

对我来说,它的工作使用。

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do

它愉快地映射到Spring控制器上。

@RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
        logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
        return "JSON Received";
}