#java #spring #azure #azure-functions
#java #spring #azure #azure-функции
Вопрос:
После запуска azure-functions:run target с помощью mvn я запускаю функцию 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
Ответ №1:
пожалуйста, обратитесь ниже 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>
Ответ №2:
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());
}
Комментарии:
1. Хотя этот код может ответить на вопрос, предоставление дополнительного контекста о том, как и / или почему он решает проблему, улучшит долгосрочную ценность ответа.