Spring MVC 3.1 REST服务post方法返回415

Spring MVC 3.1 REST服务post方法返回415,rest,post,spring-mvc,http-status-code-415,Rest,Post,Spring Mvc,Http Status Code 415,我正在做一个SpringMVC控制器,但我仍然遇到后期操作的问题。 我已经阅读了许多关于stackoverflow的解决方案,但没有解决我的问题 我目前的成就: 我发送了一个Id为的GET请求,并返回了一个成功转换为JSON的对象 我无法发送带有JSON正文的POST请求,return=415不支持的\u MEDIA\u TYPE 1) 我在pom.xml中添加了Jackson API:1.8.5 2) 我的Spring配置文件: 我添加了所有必要的部分: 视图分解器 org.spring

我正在做一个SpringMVC控制器,但我仍然遇到后期操作的问题。 我已经阅读了许多关于stackoverflow的解决方案,但没有解决我的问题

我目前的成就:

  • 我发送了一个Id为的GET请求,并返回了一个成功转换为JSON的对象
  • 我无法发送带有JSON正文的
    POST
    请求,
    return=415不支持的\u MEDIA\u TYPE
1) 我在pom.xml中添加了Jackson API:1.8.5

2) 我的Spring配置文件: 我添加了所有必要的部分:

  • 视图分解器
  • org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
  • 映射JacksonHttpMessageConverter
  • mvc:注释驱动
  • 扫描我的控制器
3) 我的模型对象很简单:一个具有Id、名称和金额的帐户

@Document
public class Account implements Serializable {

    private static final long serialVersionUID = 9058933587701674803L;

    @Id
    private String id;
    private String name;
    private Double amount=0.0;

    // and all get and set methods 
4) 最后是我的简化控制器类:

@Controller
public class AdminController {

    @RequestMapping(value="/account", method=RequestMethod.POST, 
             headers = {"content-type=application/json"})
    @ResponseStatus( HttpStatus.CREATED )
    public void addAccount(@RequestBody Account account){ 
        log.debug("account from json request " + account);
    }


    @RequestMapping(value="/account/{accountId}", method=RequestMethod.GET)
    @ResponseBody
    public Account getAccount(@PathVariable("accountId") long id){
        log.debug("account from json request " + id);
        return new Account();
    }
}
5) 在客户端,我刚刚执行了curl命令: 成功
GET
命令:

curl -i -GET -H 'Accept: application/json'  http://myhost:8080/compta/account/1
POST
命令失败:

curl -i -POST -H 'Accept: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
你知道我哪里出错了吗?

试试这个:

curl -i -POST -H "Accept: application/json" -H "Content-type: application/json" -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
“不支持的媒体类型”应该是一个提示。您的
curl
命令实际上正在发送:

Content-Type: application/x-www-form-urlencoded
只需添加显式
内容类型
标题即可:

curl -v -i -POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account

谢谢你的回答,我现在得到另一个糟糕的HTTP状态400@user1842947:这意味着您发送的JSON(错误请求)不正确,Spring MVC无法解析。请为您的问题提供更多详细信息,或者(更好)将此问题标记为已回答并打开另一个问题。好的,thx,我尝试在问题中添加我的控制器类以获取更多详细信息,但我收到有关缩进代码的堆栈溢出错误消息(4个空格)我试着修复它,然后发布it@user1842947:胡乱猜测:你能试试吗:
金额:0.0
(不带双引号)?还可以尝试“id”:“1”`(带引号)。好的,我发现了我的错误:我没有在ma Account类中为id设置get和set方法。非常感谢,您的第一个答案帮助了methx获得您的答案。选项-X在我的curl命令中不返回任何结果。和Contet类型返回另一个错误的HTTP状态:400