使用REST将数据发布到MYSql db中

时间:2016-05-03 09:45:40

标签: java mysql json rest vert.x

我是Vaadin,REST和Vertx的新手。我想将数据从JSON文件发布到MYSQL数据库中。所以我创建了一个基本网址" localhost:1443"并且能够在连接db的其他项目中调用处理程序。但是不知道将我当前项目的JSON文件传递给配置MYSQL的另一个项目。

这里调用REST中定义的方法

public OutputStream receiveUpload(String filename, String mimeType) {
    FileOutputStream fos = null; // Output stream to write to
    file = new 
    File(VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() 
  +"/Jsonfile/" + filename);
    FILE_PATH = 
    VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() 
  +"/Jsonfile/" + filename;
    try{
        fos = new FileOutputStream(file);
        JSONArray products = 
       productsDataProvider.uploadCatalogs(file.toString());
        Notification.show("Succesfully converted :)");
    }
    catch (final java.io.FileNotFoundException e) {
        // Error while opening the file. Not reported here.
        e.printStackTrace();
        return null;
    }

    return fos; // Return the output stream to write to
}

这是我定义的方法,

public JSONArray uploadCatalogs(String catalogClass) {

    return dataProviderUtil.getResourceArray("gapi/upload_products", new 
      HashMap<String, String>(){
        {
            put("class", catalogClass);
        }
    });

这是另一个项目中的产品处理程序,

    router.mountSubRouter("/gapi/upload_products", new 
                     UploadCatalog(VertxInstance.get()));

这里定义了类,

public class UploadCatalog  extends AbstractRouteHandler {
    private final static Logger LOG =        
       LogManager.getLogger(UploadCatalog.class);

    public UploadCatalog(Vertx vertx) {
    super(vertx);
    this.route().handler(BodyHandler.create());
    this.get("/").handler(this::upload);
}

private void upload(RoutingContext routingContext) {
    LOG.info("hello");      
}

我可以在控制台中看到记录器信息......

我尝试了下面的一个,

private void upload(RoutingContext routingContext) {
    JsonObject paramsObject = routingContext.getBodyAsJson();
    JsonObject result = new JsonObject();

    Integer id = LocalCache.getInstance().store(new 
           QueryData("product.insert", paramsObject));
    LOG.info("Executing query:" + "product.insert" );
    vertx.eventBus().send(DatabaseService.DB_QUERY, id,
            (AsyncResult<Message<Integer>> res) -> {
                QueryData resultData = (QueryData) 
           LocalCache.getInstance().remove(res.result().body());
                if (resultData.errorFlag || 
         resultData.updateResult.getUpdated() == 0) {
                    /*result.put("status", "error").put("error", "Error 
            in creating user profile.");
                    response.putHeader("content-type", 
          "application/json").end(result.encode());*/
                } else {
                    /*LOG.info("Insert query (ms):" + 
           resultData.responseTimeInMillis);
                    this.sendNewUserProfile(data, response);*/
                }
            });
}

如何从JSON文件中插入mysql数据库中的数据?

0 个答案:

没有答案