Как вызвать / проверить Spring Boot Controllers в aws lamda локально - PullRequest
0 голосов
/ 30 сентября 2019

Я должен проверить / вызвать Spring Boot Controllers в aws lamda локально. Я использую "serverless" для локального

Вот мой "RequestHandler"

public class ReqHandler 

implements RequestStreamHandler
{
    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(OrderManagementSystemApplication.class);
        } catch (Exception e) {
            // if we fail here. We re-throw the exception to force another cold start
            e.printStackTrace();
            throw new RuntimeException("Could not initialize Spring Boot application", e);
        }
    }

    private ApplicationContext getApplicationContext(String [] args) {
        return new SpringApplicationBuilder(OrderManagementSystemApplication.class)
                .run(args);
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        handler.activateSpringProfiles("la");
        ApplicationContext app = getApplicationContext(new String[]{});
        OrderController orderController = app.getBean(OrderController.class);


        handler.proxyStream(inputStream, outputStream, context);
    }
}

, но в строке - "OrderController orderController = app.getBean (OrderController.class);"

Я получаю ниже исключения

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.serverless.InvokeBridge.invoke(InvokeBridge.java:102)
        at com.serverless.InvokeBridge.<init>(InvokeBridge.java:40)
        at com.serverless.InvokeBridge.main(InvokeBridge.java:153)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.muqit.OMS.controller.OrderController' available
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
        at org.muqit.OMS.lamdas.ReqHandler.handleRequest(ReqHandler.java:51)
        ... 7 more

вот мой Application.class.

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
//@EnableJpaAuditing
@Profile("la")
@ComponentScan(basePackages = "org.muqit.OMS.controller")
public class OrderManagementSystemApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(OrderManagementSystemApplication.class, args);
    }


    @Bean
    public OMSServiceImpl getOMSService() {
        return new OMSServiceImpl();
    }
}

вот мой POM,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>
    <groupId>org.muqit.OMS</groupId>
    <artifactId>OrderManagementSystem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OrderManagementSystem</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

        <dependency>
    <groupId>com.amazonaws.serverless</groupId>
    <artifactId>aws-serverless-java-container-springboot2</artifactId>
    <version>1.4</version>
</dependency>

       </dependencies>

    <build>
        <plugins>

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>org.apache.tomcat.embed:*</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        </plugins>
    </build>






</project>

Пожалуйста, скажите мнеесли я делаю что-то не так или если есть какой-то другой подход, мы должны проверить / вызвать restController локально, используя aws lambda без сервера.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...