spring-cloud / spring-cloud-function

Apache License 2.0
1.03k stars 615 forks source link

Application starts up as WebMVC application #1029

Closed schlumpfling42 closed 1 year ago

schlumpfling42 commented 1 year ago

I'm trying to write 2 apps, one as a standard Spring Boot App and a Spring Cloud Function. I'm trying to avoid duplicate code, so I created a shared jar that contains some configuration (that is also used by other apps). I'm having trouble with running the Spring Cloud Function app. I can deploy it in a container with the AWS adapter (I understand that it will run another app in the container), but I cannot run it locally as Cloud Function App, it starts up as a regular Spring Boot app. I think it has to do with some AutoConfiguration, but I'm not certain. I also tried to run some tests, but they don't work either.

I'm using Spring Boot 3.0.5 and Cloud Function 4.0.2.

Here is the app:

@SpringBootApplication
@Log4j2
@Import({PostgresDatasourceConfiguration.class})
public class FunctionsApplication {

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

    @Bean
    public Function<String, String> ping() {
        return value -> {
            return value == null ? "pong" : value;
        };
    }

}

I have only some configuration entries for postgres in my properties field.

Here are the dependencies in the gradle build file:

dependencies {
    implementation "org.springframework.cloud:spring-cloud-function-web:${springCloudFunctionVersion}"
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws:3.2.1'
    implementation "com.amazonaws:aws-lambda-java-events:${awsLambdaEventsVersion}"
    implementation "com.amazonaws:aws-lambda-java-core:${awsLambdaCoreVersion}"

    implementation project(':shared')

    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.postgresql:postgresql'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.mockito:mockito-junit-jupiter:5.2.0'
}

!!!UPDATE: The shared code has the following dependencies that cause the issue:

    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'

Just the presence of these imports will trigger it to not start up the Function handler.

Here is the console output when I start up:

2023-05-02 07:32:20-930 [main] INFO  o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-02 07:32:20-935 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-02 07:32:21-057 [main] DEBUG o.s.b.a.AutoConfigurationPackages - @EnableAutoConfiguration was declared on a class in the package 'net.functional.test'. Automatic @Repository and @Entity scanning is enabled.
2023-05-02 07:32:21-261 [main] WARN  o.s.b.a.o.j.JpaBaseConfiguration$JpaWebConfiguration - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-02 07:32:21-393 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerAdapter - ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2023-05-02 07:32:21-430 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - 2 mappings in 'requestMappingHandlerMapping'
2023-05-02 07:32:21-442 [main] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2023-05-02 07:32:21-447 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - ControllerAdvice beans: 1 @ExceptionHandler, 1 ResponseBodyAdvice
2023-05-02 07:32:21-471 [main] DEBUG o.s.b.a.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin - Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'
2023-05-02 07:32:21-560 [main] WARN  o.s.b.a.s.s.UserDetailsServiceAutoConfiguration - 

Using generated security password: d455b419-5f6a-4228-9899-39cadd784b25

This generated password is for development use only. Your security configuration must be updated before running your application in production.

2023-05-02 07:32:21-648 [main] INFO  o.s.s.web.DefaultSecurityFilterChain - Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@70a3b, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@18baee88, org.springframework.security.web.context.SecurityContextHolderFilter@6e4ac3f5, org.springframework.security.web.header.HeaderWriterFilter@1ab780fd, org.springframework.security.web.csrf.CsrfFilter@642b2176, org.springframework.security.web.authentication.logout.LogoutFilter@f4b2d13, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@4d5d945d, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@78ca8258, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@59e67a18, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@4e2ad1f2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1798c749, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@33315925, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@37289e90, org.springframework.security.web.access.ExceptionTranslationFilter@53ce27fa, org.springframework.security.web.access.intercept.AuthorizationFilter@49314684]
2023-05-02 07:32:21-673 [main] INFO  o.s.c.f.w.mvc.FunctionHandlerMapping - FunctionCatalog: org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry@21f27bb5
2023-05-02 07:32:21-697 [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''

Here is the test I wrote:

@SpringBootTest(classes = FunctionsApplication.class,
    webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class WebFunctionTests {

    @Autowired
    private TestRestTemplate rest;

    @Test
    public void test() throws Exception {
        ResponseEntity<String> result = this.rest.exchange(
            RequestEntity.post(new URI("/ping")).body("{}}"), String.class);
        System.out.println(result.getBody());
    }
}

Any insight on how to get it running locally would be great.

schlumpfling42 commented 1 year ago

OK ... figured it it (sometimes you have to spell it all out :-| ). The shared library I had different kinds of configuration classes and its dependencies are the issue, even so my code is not using any security configuration (excluded by a conditional) the added imports are doing some auto configuration that will switch the app to a regular MVC app.

If I exclude these to dependencies, everything is working fine.

    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'

Closing the issue.