我有这个json POST url'http://localhost:8080/demo/test' from PHP
'{"postby_id":"1","title":"ftkjhg","is_private":"0","status":"1","post_type_id":"1","type":"2","p_id":"1","ve_id":"3","link":"1111111"}'
我的网络控制器
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
void creMysqlCall() throws Exception {
creativityService.creMysqlCall();
}
服务文件
public void creMysqlCall() throws Exception {
creativityDao.creMysqlCall();
}
我的DAO文件
public void creMysqlCall() throws Exception {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(masterJdbcTemplate).withTableName("posts")
.usingColumns("postby_id","title","is_private","post_type_id","status","wall_type","p_id","ve_id","link");
Map<String, Object> creInsertMap = Maps.newHashMap();
creInsertMap.put("postby_id", "");
creInsertMap.put("title", "");
creInsertMap.put("is_private", "");
creInsertMap.put("post_type_id", "");
creInsertMap.put("status", "");
creInsertMap.put("wall_type", "");
creInsertMap.put("p_id", "");
creInsertMap.put("ve_id", "");
creInsertMap.put("link", "");
我如何将数据从 URL 发布到这个 DAO 我是 Java 新手提前感谢
更新
收到此错误
HTTP 错误 405访问/后端/测试时出现问题。原因: 不支持请求方法"GET"
使用 getter 和 setter 的新类文件
public class MysqlCall {
public Object is_private;
public Object postby_id;
public Object title;
public Object getPostby_id() {
return postby_id;
}
public void setPostby_id(Object postby_id) {
this.postby_id = postby_id;
}
public Object getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Object getIs_private() {
return is_private;
}
public void setIs_private(String is_private) {
this.is_private = is_private;
}
public Object getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getPost_type_id() {
return post_type_id;
}
public void setPost_type_id(String post_type_id) {
this.post_type_id = post_type_id;
}
public Object getWall_type() {
return wall_type;
}
public void setWall_type(String wall_type) {
this.wall_type = wall_type;
}
public Object getPostto_id() {
return postto_id;
}
public void setPostto_id(String postto_id) {
this.postto_id = postto_id;
}
public Object getVertical_id() {
return vertical_id;
}
public void setVertical_id(String vertical_id) {
this.vertical_id = vertical_id;
}
public Object getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Object status;
public Object post_type_id;
public Object wall_type;
public Object postto_id;
public Object vertical_id;
public Object link;
新道
public void creMysqlCall(MysqlCall call) throws Exception {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(masterJdbcTemplate).withTableName("posts")
.usingColumns("postby_id","title","is_private","post_type_id","status","wall_type","postto_id","vertical_id","link");
Map<String, Object> creInsertMap = Maps.newHashMap();
creInsertMap.put("postby_id", call.postby_id);
creInsertMap.put("title", call.title);
creInsertMap.put("is_private", call.is_private);
creInsertMap.put("post_type_id", call.post_type_id);
creInsertMap.put("status", call.status);
creInsertMap.put("wall_type", call.wall_type);
creInsertMap.put("postto_id", call.postto_id);
creInsertMap.put("vertical_id", call.vertical_id);
creInsertMap.put("link", call.link);
服务文件
public void creMysqlCall(MysqlCall call) throws Exception {
creativityDao.creMysqlCall(call);
}
控制器
用于测试服务调用的 API@RequestMapping(值 = "/test", 方法 = RequestMethod.POST, 消耗 = MediaType.APPLICATION_JSON_VALUE)
void creMysqlCall(@RequestBody(required = true) MysqlCall call ) 抛出异常 {
creativityService.creMysqlCall(call);
}
您需要使用 getter 和 setter 从 JSON 创建一个值对象类。
然后您的控制器将看起来像
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
void creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {
creativityService.creMysqlCall(call);
}
确保你的班级路径中有杰克逊,所以在你的pom
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
这
更像是一个Spring MVC和Jackson的问题。你必须创建一个类,如下所示:
public class MysqlCall {
private String postby_id;
/* the rest of the fields in JSON */
/* getters and setters */
}
然后使用 @RequestBody 注释对其进行映射:
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {
//creativityService.creMysqlCall(call);
return call;
}
在 DAO 中,您只需将对象参数与 sql 映射链接即可。