【发布时间】:2018-02-27 14:27:44
【问题描述】:
嗯。我需要在我的后端创建函数来与我的前端进行通信。从我的后端获取信息的方法可以正常工作。但我不知道如何在我的后端读取 json 信息。
这是我的get方法:
@RequestMapping("/hi")
public @ResponseBody String getName(){
UserInfo info = new UserInfo("Lucas Alves",21,"luke@email.com","99998888");
Gson g = new Gson();
return g.toJson(info);
}
我正在使用 gson 库将我的对象 UserInfo 转换为 json 字符串。
现在我的问题是。如何获取从前端发送给我并在 UserInfo 中安全的 json 文本。
打字稿代码:
postUser(p: User){
return this.http.post('http://localhost:8090/postUser', JSON.stringify(p), { headers: new Headers({ 'Content-Type': 'application/json' }) });
}
Java 代码:
@RequestMapping(value = {"/postUser"}, method = RequestMethod.POST)
public void createUser( What I need to have here? ) {
//I Don't know what I need to do here.
}
【问题讨论】:
-
你正在发送 json 所以,我认为,它应该是
@RequestBody UserInfo input,但如果你的 jsUser不是一对一匹配,你可以创建一些 DTO 类
标签: json spring angular post spring-boot