插入数据从POST卷曲JAVA春

问题描述:

我有此JSON帖子的网址 'http://localhost:8080/demo/test' 从PHP插入数据从POST卷曲JAVA春

'{"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(值= “/测试”,方法= RequestMethod.POST,消耗= MediaType.APPLICATION_JSON_VALUE)

空隙creMysqlCall(@RequestBody(必需=真)MysqlCall呼叫)抛出异常{

creativityService.creMysqlCall(call); 

}

+0

使用工具,如邮差或高级REST客户端(包括有铬扩展) 。除非API收到查询参数(用于GET),否则您无法从浏览器的URL执行此操作。 – Mubin

这更是一个弹簧MVC的210杰克逊的问题。 你必须创建一个类,如:

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; 
} 

在您刚才使用SQL映射链接对象PARAMS的DAO。

+0

有问题请检查更新提前致谢 – gopu0000

+0

也许错误的解释会有所帮助。 – sahel

+0

得到此错误 HTTP错误405访问/后端/测试问题。原因:不支持请求方法'GET' – gopu0000

您将需要使用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); 
} 

请确保您有杰克逊在类路径中,所以在你的POM

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.6.0</version> 
</dependency> 
+0

有错误,请提前检查更新 – gopu0000