【问题标题】:Deployment of Azure Function in Visual Studio Code does not include .jar file在 Visual Studio Code 中部署 Azure Function 不包含 .jar 文件
【发布时间】:2020-08-06 18:06:30
【问题描述】:

我正在构建的 Azure 函数需要能够在 Azure SQL Server 数据库中执行过程。

我在 Eclipse 中有工作 Java 代码(基于 @duffmo 在Java DB connection 中的回答)

然后我将代码移植到 Visual Studio Code 中的 Azure 函数,以部署到 Azure。 (注意我已经删除了安全代码等)我使用View/Command Palette/Azure Functions - Create New Project创建了这个项目

package com.function;

import java.sql.*;
import java.util.*;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/HttpTrigger-Java". Two ways to invoke
     * it using "curl" command in bash: 1. curl -d "HTTP Body" {your
     * host}/api/HttpTrigger-Java&code={your function key} 2. curl "{your
     * host}/api/HttpTrigger-Java?name=HTTP%20Query&code={your function key}"
     * Function Key is not needed when running locally, it is used to invoke
     * function deployed to Azure. More details:
     * https://aka.ms/functions_authorization_keys
     */
    private static final String DEFAULT_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String DEFAULT_URL = "jdbc:sqlserver://myserver.database.windows.net:1433;database=mydb;loginTimeout=10;user=myuser@myserver;password=mypassword;";


    @FunctionName("HttpTrigger-Java")
    public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
            HttpMethod.POST }, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        Connection connection = null;
        try {

            connection = createConnection(DEFAULT_DRIVER, DEFAULT_URL,context);
            connection.setAutoCommit(false);
            String sqlUpdate = "{call MYDB.MYPROC(?,?}";
            List<Object> parameters = Arrays.asList("Bar", "Foo");
            execute(connection, sqlUpdate, parameters);
            connection.commit();

        } catch (Exception e) {
            rollback(connection);
            e.printStackTrace();
        } finally {
            close(connection);
        }
        return null;
    }

    public static Connection createConnection(String driver, String url, ExecutionContext context) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection(url);
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void rollback(Connection connection) {
        try {
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static double execute(Connection connection, String sql, List<Object> parameters) throws SQLException {
        CallableStatement call = connection.prepareCall(sql);
        try {
            int i = 0;
            for (Object parameter : parameters) {
                call.setObject(++i, parameter);
            }
            call.executeUpdate();

        } finally {
            close(call);
        }
        return 0;
    }
}

然而行

 Class.forName(driver);

导致以下错误

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at java.base/java.lang.Class.forName0(Native Method)

我试图解决这个问题

  1. sqljdbc4.jar 放在“lib”目录中
  2. 手动将以下内容添加到 pom.xml

    &lt;dependency&gt; &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt; &lt;artifactId&gt;sqljdbc4&lt;/artifactId&gt; &lt;version&gt;4.0&lt;/version&gt; &lt;/dependency&gt;

  3. 尝试从终端通过install the jar

    mvn install:install-file -Dfile=’C:=myPath\myFunction\lib\sqljdbc4.jar' -DgroupId=package -DartifactId=sqljdbc4 -Dversion='4.0' -Dpackaging=jar

  4. experimented 更改 DEFAULT_DRIVER 字符串中“microsoft”和“sqlserver”的顺序。

  5. 尝试从新的Java Dependencies view 添加 SQLJDBC(请参阅@hemangs 的答案)-但它没有出现在列表中

  6. 我根据@asndr 在.classpath 中的回答编辑了 .classPath - 请注意,我没有设法从 VS Code 中访问 .classPath,而是通过文件资源管理器 - 然后运行 ​​view/Command Palette/Java: Clean the java language server workspace

有什么想法吗?

【问题讨论】:

    标签: java sql-server azure visual-studio-code azure-functions


    【解决方案1】:

    基于@nirmal 在Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0 中的回答 我做了以下事情:

    1. Explorer/Java Dependencies/Maven Dependencies - 然后点击“+”
    2. 输入mssql-jdbc 并按Enter
    3. 从 com.microsoft.sqlserver 中选择 mssql-jdbc
    4. 这会在 VS Code 中打开 pom.xml,并添加以下内容

      &lt;dependency&gt;

        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>8.3.0.jre14-preview</version>
      </dependency>
      
    5. 我把版本号改成了6.1.0.jre8(高版本导致编译错误)

    6. 已保存
    7. VS Code 询问我是否要“修改了构建文件。是否要同步 Java 类路径/配置?'
    8. 我说是的,然后就成功了。

    似乎至关重要的是从 VS Code 中编辑 pom.xml。看来当我在 VS Code 之外编辑它时,VS Code 并没有触发配置的同步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多