2016-03-26 49 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/