如何向我的 Spring REST 项目发出正确的 POST 请求
How make a correct POST request to my Spring REST project
我面临的问题是当我尝试执行 POST 请求时,例如:
curl -H "Content" -X POST -d '{"content":"abc"}' http://localhost:8080/posts
我最终得到一个新的 post 但没有任何数据:
{
id: 4,
content: null,
comment: [ ]
}
我有一些来自 SQL 文件的预加载数据(我在我的项目中启用了 H2):
INSERT INTO post(id, content) VALUES (1, 'This is a post')
INSERT INTO post(id, content) VALUES (2, 'This is a post')
INSERT INTO post(id, content) VALUES (3, 'This is a post')
我的实体:
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
private Set<Comment> comment;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Set<Comment> getComment() {
return comment;
}
public void setComment(Set<Comment> comment) {
this.comment = comment;
}
}
来自我的控制器:
@PostMapping("/posts")
public @ResponseBody String add(@ModelAttribute Post p){
postRepository.save(p);
return "{msg : Resource added }";
}
在我对这个问题的研究中,我了解到你可以使用 RestController,但是我没有在我的示例中使用它,因为我的代码来自我的学校项目,老师希望我们使用 @ResponseBody 等。
我的@PostMapping 方法是否遗漏了什么?还是我的 curl 语句有误?
@ModelAttibute
在您的案例中不起作用的主要原因是它受限于表单 post 参数,而您的案例中没有表单。
在 @RequestBody
中,您可以直接获取请求正文。
这就是您 @ModelAttribute
无法正常工作
的原因
要使用 @ModelAttribute
,您必须使用 modelattribute
创建一个表单
类似下面的内容
<form:form method="POST" action="/posts"
modelAttribute="posts">
<form:label path="name">Name</form:label>
<form:input path="name" />
<form:label path="id">Id</form:label>
<form:input path="id" />
<input type="submit" value="Submit" />
</form:form>
然后在你的控制器中,像
@PostMapping("/posts")
public @ResponseBody String add(@ModelAttribute("posts") Post p){
postRepository.save(p);
return "{msg : Resource added }";
}
但是当您使用 curl
来满足您的要求时,最适合的是 @RequestBody
获取 post 参数。
进一步阅读here
我面临的问题是当我尝试执行 POST 请求时,例如:
curl -H "Content" -X POST -d '{"content":"abc"}' http://localhost:8080/posts
我最终得到一个新的 post 但没有任何数据:
{
id: 4,
content: null,
comment: [ ]
}
我有一些来自 SQL 文件的预加载数据(我在我的项目中启用了 H2):
INSERT INTO post(id, content) VALUES (1, 'This is a post')
INSERT INTO post(id, content) VALUES (2, 'This is a post')
INSERT INTO post(id, content) VALUES (3, 'This is a post')
我的实体:
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
private Set<Comment> comment;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Set<Comment> getComment() {
return comment;
}
public void setComment(Set<Comment> comment) {
this.comment = comment;
}
}
来自我的控制器:
@PostMapping("/posts")
public @ResponseBody String add(@ModelAttribute Post p){
postRepository.save(p);
return "{msg : Resource added }";
}
在我对这个问题的研究中,我了解到你可以使用 RestController,但是我没有在我的示例中使用它,因为我的代码来自我的学校项目,老师希望我们使用 @ResponseBody 等。
我的@PostMapping 方法是否遗漏了什么?还是我的 curl 语句有误?
@ModelAttibute
在您的案例中不起作用的主要原因是它受限于表单 post 参数,而您的案例中没有表单。
在 @RequestBody
中,您可以直接获取请求正文。
这就是您 @ModelAttribute
无法正常工作
要使用 @ModelAttribute
,您必须使用 modelattribute
类似下面的内容
<form:form method="POST" action="/posts"
modelAttribute="posts">
<form:label path="name">Name</form:label>
<form:input path="name" />
<form:label path="id">Id</form:label>
<form:input path="id" />
<input type="submit" value="Submit" />
</form:form>
然后在你的控制器中,像
@PostMapping("/posts")
public @ResponseBody String add(@ModelAttribute("posts") Post p){
postRepository.save(p);
return "{msg : Resource added }";
}
但是当您使用 curl
来满足您的要求时,最适合的是 @RequestBody
获取 post 参数。
进一步阅读here