2016-03-26 86 views
0

我在我的項目中使用swagger生成代碼來執行某些objcets(命名項目)的http請求(GET,POST和DELETE)。如何使用H2數據庫與swagger

這裏的問題是這些對象是在內存中創建的。我的意思是,當我重新啓動我的Web服務時,以前的更改將被刪除。所以,我想使用(本地)數據庫來保存這些更改。我對H2數據庫非常感興趣。我也想用JDBC來連接我的本地數據庫。這裏的主要問題是我不明白如何首先使用jdbc。其次,招搖把所產生的代碼對我來說太複雜了。我不知道我應該在哪裏做jdbc代碼...

我在這裏複製了用於兩個請求(例如POST和DELETE)的代碼。你能幫我做一下當地的h2數據庫嗎?我應該在這個班上做這個代碼嗎?

這裏是招搖的部分代碼:

@POST 
@Consumes({"application/x-www-form-urlencoded"}) 
@Produces({"application/json"}) 
@io.swagger.annotations.ApiOperation(value = "", notes = "Creates a project and returns the whole object", response = Project.class, tags = {}) 
@io.swagger.annotations.ApiResponses(value = { 
@io.swagger.annotations.ApiResponse(code = 200, message = "Created project", response = Project.class), 
@io.swagger.annotations.ApiResponse(code = 500, message = "Internal API error/Server error", response = Project.class)}) 
public Response createProject(
@ApiParam(value = "Document name", required = true) @FormParam("name") String name, 
    @ApiParam(value = "Document title") @FormParam("trigram") String trigram, @Context SecurityContext securityContext) 
    throws NotFoundException { 
    return delegate.createProject(name, trigram, securityContext); 
    } 

    //url is localhost:8080/api/v1/projects/{id} 
    @DELETE 
    @Path("/{id}") 
    @io.swagger.annotations.ApiOperation(value = "", notes = "Deletes all projects", response = void.class, tags={ }) 
    @io.swagger.annotations.ApiResponses(value = { 
    @io.swagger.annotations.ApiResponse(code = 200, message = "Project is deleted.", response = void.class), 
    @io.swagger.annotations.ApiResponse(code = 500, message = "Internal API error/Server error", response = void.class) }) 
    public Response deleteProject(
    @ApiParam(value = "Document ID",required=true) @PathParam("id") String id,@Context SecurityContext securityContext) 
    throws NotFoundException { 
    return delegate.deleteProject(id,securityContext); } 
+0

是你能解決這個@ salamanka44? – Sampada

回答

0

你應該創建一個單獨的dataobjectaccess(DAO)類,以使該數據庫的連接。您可以在問題中包含的課程中調用它的方法。

事實上,基本的方法是甚至分開swagger接口和實現類,以便更易於閱讀。這樣,您的RESTful API將真正成爲與所有註釋在一個地方的界面。實現類可以包含代碼,然後調用DAO類來連接到數據庫。

各種教程可供學習jdbc具體到H2:

http://www.h2database.com/html/tutorial.html http://www.ayukucode.org/2012/03/16/create-table-insert-query-with-jdbc-and-h2-database/