Insert Data from POST Curl to JAVA Spring
i have this 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"}'
my webController
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
void creMysqlCall() throws Exception {
creativityService.creMysqlCall();
}
Service file
public void creMysqlCall() throws Exception {
creativityDao.creMysqlCall();
}
myDAO file
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", "");
how can i post data from URL to this DAO am new to java thanks in advance
UPDATE
got this error
HTTP ERROR 405 Problem accessing /backend/test. Reason: Request method 'GET' not supported
New Class file using getter and 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;
NEw 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);
Service file
public void creMysqlCall(MysqlCall call) throws Exception {
creativityDao.creMysqlCall(call);
}
Controller
// API to test service call @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
void creMysqlCall(@RequestBody(required = true) MysqlCall call ) throws Exception {
creativityService.creMysqlCall(call);
}
You will need to create a Value Object class from your JSON with getter and setters.
Then your controller will look like
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
void creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {
creativityService.creMysqlCall(call);
}
Make sure you have Jackson in your classpath, so in your pom
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
This is more of a Spring MVC & Jackson question. You have to create a class like:
public class MysqlCall {
private String postby_id;
/* the rest of the fields in JSON */
/* getters and setters */
}
And then use the @RequestBody annotation to map it:
@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;
}
In the DAO you just link the object params with the sql mapping.
链接地址: http://www.djcxy.com/p/25576.html上一篇: 列出来自命令行的所有环境变量?