JSON POST 的 Http 415 不支持的媒体类型错误

Http 415 Unsupported Media type error with JSON POST

我正在尝试执行项目 https://github.com/bava/springrest-book/tree/master/Chapter4/final/quick-poll-ch4-final

我的控制器是 https://github.com/bava/springrest-book/blob/master/Chapter4/final/quick-poll-ch4-final/src/main/java/com/apress/controller/PollController.java

我所做的唯一改变是使用 Lombok(目前不在 Poll class 中)

@ToString   
@Entity
public class Option {
    @Id
    @GeneratedValue
    @Column(name="OPTION_ID")
    @Getter 
    @Setter
    private Long id;

    @Column(name="OPTION_VALUE")
    @Getter 
    @Setter
    private String value;
}

我遇到错误

{"timestamp":1448542212469,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'text/plain;charset=UTF-8' not supported","path":"/polls"}

发送 POST 请求时

{
    "question": "Who will win SuperBowl this year?",
    "options": [
                       {"value": "New England Patriots"},
                       {"value": "Seattle Seahawks"},
                       {"value": "Green Bay Packers"},
                       {"value": "Denver Broncos"}]
}

不确定我做错了什么 注意 : Get 工作正常(现在只能用 0 个条目尝试)

您必须为您提出的请求指定 Content-type,如下所示。