2016-03-03 100 views
0

我有此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

回答

0

這更是一個彈簧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

0

您將需要使用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