[英]Spring Cloud Function in Azure throws IllegalStateException upon triggering
使用 mvn 運行 azure-functions:run target 后,我觸發了端點上的 Azure 函數:
你好:[GET,POST] http://localhost:7071/api/hello
在日志中顯示 Spring 啟動並出現以下錯誤,我無法理解其原因:
[2020-11-10T10:56:35.253Z] Handler processing a request for: hello
[2020-11-10T10:56:35.272Z] Executed 'Functions.hello' (Failed, Id=403ee5e4-464b-4d0a-bd56-d6ade21d2de8, Duration=1103ms)
[2020-11-10T10:56:35.272Z] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
[2020-11-10T10:56:35.272Z] Exception: IllegalStateException: No function defined
[2020-11-10T10:56:35.272Z] Stack: java.lang.reflect.InvocationTargetException
[2020-11-10T10:56:35.272Z] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2020-11-10T10:56:35.272Z] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[2020-11-10T10:56:35.272Z] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[2020-11-10T10:56:35.272Z] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.broker.EnhancedJavaMethodExecutorImpl.execute(EnhancedJavaMethodExecutorImpl.java:55)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:57)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
[2020-11-10T10:56:35.272Z] at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
[2020-11-10T10:56:35.272Z] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[2020-11-10T10:56:35.272Z] at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[2020-11-10T10:56:35.272Z] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[2020-11-10T10:56:35.272Z] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[2020-11-10T10:56:35.272Z] at java.base/java.lang.Thread.run(Thread.java:834)
[2020-11-10T10:56:35.272Z] Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalStateException: No function defined
[2020-11-10T10:56:35.272Z] at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.handleRequest(AzureSpringBootRequestHandler.java:99)
[2020-11-10T10:56:35.272Z] at com.dropware.function.HelloHandler.execute(HelloHandler.java:28)
[2020-11-10T10:56:35.272Z] ... 16 more
[2020-11-10T10:56:35.272Z] Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: No function defined
[2020-11-10T10:56:35.272Z] at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.handleRequest(AzureSpringBootRequestHandler.java:99)
[2020-11-10T10:56:35.272Z] at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.handleRequest(AzureSpringBootRequestHandler.java:83)
[2020-11-10T10:56:35.272Z] ... 17 more
[2020-11-10T10:56:35.272Z] Caused by: java.lang.IllegalStateException: No function defined
我有兩個域類:
public class Greeting {
public Greeting() {
}
public Greeting(String message) {
this.message = message;
}
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class User {
public User() {
}
public User(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
該函數是 HTTP 觸發的,它完全遵循此處提供的示例
public class HelloHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hello")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return request
.createResponseBuilder(HttpStatus.OK)
.body(handleRequest(request.getBody().get(), context))
.header("Content-Type", "application/json")
.build();
}
}
Spring 的切入點非常簡單。
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Bean
public Function<User, Greeting> hello() {
return user -> new Greeting("Welcome, " + user.getName());
}
}
pom.xml 文件中的依賴項是:
org.junit.jupiter
junit-jupiter
5.4.2
org.mockito</groupId>
mockito-core
2.23.4
org.springframework.boot
spring-boot-starter-test
2.3.5.RELEASE
com.microsoft.azure.functions
azure-functions-java-library
1.3.1
org.springframework.cloud
spring-cloud-function-adapter-azure
3.0.11.RELEASE
org.springframework.cloud
spring-cloud-function-web
3.0.11.RELEASE
org.springframework.boot
spring-boot-starter-jdbc
2.3.5.RELEASE
org.springframework.boot
spring-boot-devtools
2.3.5.RELEASE
org.springframework.boot
spring-boot-configuration-processor
2.3.4.RELEASE
mysql
mysql-connector-java
8.0.22
請參考下面的 pom.xml 並確保您沒有遺漏任何字段和應用程序設置。
<property>
<name>WEBSITE_RUN_FROM_PACKAGE</name>
<value>1</value>
</property>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~3</value>
</property>
<property>
<name>FUNCTIONS_WORKER_RUNTIME</name>
<value>java</value>
</property>
Try to provide function name, if there are multiple fucntion need to be executed aa follows
@Bean("hello")
public Function<User, Greeting> hello() {
return user -> new Greeting("Welcome, " + user.getName());
}
@Bean("GoodMorning")
public Function<User, Greeting> goodMorning() {
return user -> new Greeting("Good Morning " + user.getName());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.