serverless-java-container icon indicating copy to clipboard operation
serverless-java-container copied to clipboard

Swagger file generation on Spring Boot lambda

Open thomashoef opened this issue 4 years ago • 2 comments

  • Implementations: Spring Boot

Scenario

I have annotated my spring boot 2 lambda with a Swagger2Config so that it auto-generates a Swagger-file for the annotated methods in the controller. Is there a way to access the swagger-ui.html after deploying the lambda? The annotated request mapping are reacheable just fine.

Steps to reproduce

Add these maven dependencies to your spring boot 2 mvn lambda:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>

Add a Swagger2Config file and annotate the controller methods with Swagger annotations.

    @RequestMapping(path = "/categories", method = RequestMethod.POST)
    @ApiOperation(value="Add a new category",response=Category.class)
    @ApiResponses(value={
            @ApiResponse(code=200,message="Category created successfully",response=Category.class, responseContainer="List"),
            @ApiResponse(code=500,message="Internal server error")
    })

thomashoef avatar Apr 21 '20 09:04 thomashoef

I get it working by adding a config class to add resources hanlders. And I'm using REST API with lambda proxy integration enabled.


@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");
    }
}

bnusunny avatar Apr 23 '20 15:04 bnusunny

My question is related but I am actually looking to remove the swagger generation code from the code base and when I remove the Configuration file I get the following error only in Lambda execution:

`2020-11-20T08:08:22.157-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse - Response buffer flushed with 0 bytes, latch=1
2020-11-20T08:08:22.157-05:00 2020-11-20.13:08:22 [main] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.XXXX.common.model.GenericResponse]
2020-11-20T08:08:22.158-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletRequest - Trying to access session. Lambda functions are stateless and should not rely on the session
2020-11-20T08:08:22.160-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter
2020-11-20T08:08:22.162-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-springSecurityFilterChain
2020-11-20T08:08:22.162-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-requestContextFilter
2020-11-20T08:08:22.162-05:00 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-formContentFilter
2020-11-20T08:08:22.162-05:00 Copy 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-characterEncodingFilter 2020-11-20.13:08:22 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder - Executed ERROR: filter 6-characterEncodingFilter`

The steps followed are:

  1. Remove the class which has public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter

  2. Remove the springfox dependencies from the lambda packaging. ` <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> 2.9.2

     <dependency>
     	<groupId>io.springfox</groupId>
     	<artifactId>springfox-swagger-ui</artifactId>
     	<version>2.9.2</version>
     </dependency>`
    

patvin80 avatar Nov 20 '20 14:11 patvin80