我用几种不同的方式检查过,还下载了一个新项目,看看要检查什么是错误,但我仍然不知道答案。
那是我的 RestController
@RestController
@RequestMapping(value = "/message")
public class MessageController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public void createMessage(@RequestBody Message message){
System.out.println(message);
}
}
那是我的模特
@Data
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String sender;
private String telephone;
private String message;
}
如有必要,Gradle 依赖项
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0.pr3'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
在邮递员中我收到了那个错误
{ “时间戳”:1495992553884,“状态”:415,“错误”:“不支持的媒体类型”,“异常”:“org.springframework.web.HttpMediaTypeNotSupportedException”,
“message”: “不支持内容类型’application/x-www-form-urlencoded;charset=UTF-8’”,
“路径”:“/消息/”}
这是最简单的休息方式,但我在哪里犯了错误?
原文由 jodekpotasu 发布,翻译遵循 CC BY-SA 4.0 许可协议
在邮差。在
Body
下,选择raw
并从出现的下拉菜单中选择 JSON。然后编写作为请求主体的 JSON。 You can’t useform-data
orx-www-form-urlencoded
with@RequestBody
, they are used when the binding is@ModelAttribute
.