从POST Curl插入数据到JAVA Spring
我有这个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"}'
我的webController
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
void creMysqlCall() throws Exception {
creativityService.creMysqlCall();
}
服务文件
public void creMysqlCall() throws Exception {
creativityDao.creMysqlCall();
}
myDAO文件
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感谢提前
UPDATE
有这个错误
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;
新的DAO
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(value =“/ test”,method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
void creMysqlCall(@RequestBody(required = true)MysqlCall调用)抛出Exception {
creativityService.creMysqlCall(call);
}
您需要使用getter和setter从JSON中创建一个Value Object类。
那么你的控制器会看起来像
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
void creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {
creativityService.creMysqlCall(call);
}
确保你的班级中有Jackson,所以在你的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映射。
链接地址: http://www.djcxy.com/p/25575.html