springdoc / springdoc-openapi-maven-plugin

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
141 stars 37 forks source link

java.net.ConnectException: Connection refused (Connection refused) #6

Closed paul-ovchinnikov closed 4 years ago

paul-ovchinnikov commented 4 years ago

Output log for mvn clean verify -Dspring.application.admin.enabled=true below

Application was started

INFO [is,,,] 7851 --- [           main] o.e.jetty.server.AbstractConnector       : Started ServerConnector@3dc82e6a{HTTP/1.1,[http/1.1]}{0.0.0.0:10000}
INFO [is,,,] 7851 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 10000 (http/1.1) with context path '/'
INFO [is,,,] 7851 --- [           main] com.cdek.is.IsApplication                : Started IsApplication in 10.874 seconds (JVM running for 12.087)

springdoc-openapi-maven-plugin:0.3:generate

INFO] --- springdoc-openapi-maven-plugin:0.3:generate (integration-test) @ is ---
[ERROR] An error has occured
java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect (Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392)
    at java.net.Socket.connect (Socket.java:589)
    at java.net.Socket.connect (Socket.java:538)
    at sun.net.NetworkClient.doConnect (NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer (HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer (HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init> (HttpClient.java:242)
    at sun.net.www.http.HttpClient.New (HttpClient.java:339)
    at sun.net.www.http.HttpClient.New (HttpClient.java:357)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient (HttpURLConnection.java:1220)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0 (HttpURLConnection.java:1156)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect (HttpURLConnection.java:1050)
    at sun.net.www.protocol.http.HttpURLConnection.connect (HttpURLConnection.java:984)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0 (HttpURLConnection.java:1564)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream (HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode (HttpURLConnection.java:480)
    at org.springdoc.maven.plugin.SpringDocMojo.execute (SpringDocMojo.java:43)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:47)

pom.xml

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <wait>1000</wait>
                    <maxAttempts>180</maxAttempts>
                    <jvmArguments>
                        -Dspring.application.admin.enabled=true
                    </jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-maven-plugin</artifactId>
                <version>0.3</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
randyhbh commented 4 years ago

You are missing this in your project, is Kotlin code but I guess you will understand the idea

@Configuration
class SecurityConfig() : WebSecurityConfigurerAdapter() {

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
            http
            .csrf().disable()
            .antMatcher("/**")
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and()
            .authorizeRequests()
            .antMatchers("/v3/api-docs").permitAll()
            .anyRequest().denyAll()
            .and()
            .httpBasic()
    }
}
saurabhpro commented 4 years ago

You are missing this in your project, is Kotlin code but I guess you will understand the idea

@Configuration
class SecurityConfig() : WebSecurityConfigurerAdapter() {

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
            http
            .csrf().disable()
            .antMatcher("/**")
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.NEVER)
            .and()
            .authorizeRequests()
            .antMatchers("/v3/api-docs").permitAll()
            .anyRequest().denyAll()
            .and()
            .httpBasic()
    }
}

could you elaborate a little, I cant translate it to my Java project

randyhbh commented 4 years ago

So, your problem is that you are not allowing access to the route where the swagger is exposed. You need to create a configuration class and extend from WebSecurityConfigurerAdapter and override the method configure(HttpSecurity http){....} and allow the access to "/v3/api-docs" path

poteii commented 3 years ago

If we run the spring context from a war deployment(No main class), How to solve java.net.ConnectException: Connection refused (Connection refused)?

randyhbh commented 3 years ago

The solution will be the same. You just need to find out how to expose the route "/v3/api-docs" for a war deployment, and I will assume is quite similar

poteii commented 3 years ago

Hi @RandyHBH I've already to add route "/v3/api-docs" both of WebSecurityConfigurerAdapter class and yaml file. but it still has Connection Refuse. This is my pom.

org.springframework.boot spring-boot-maven-plugin pre-integration-test start post-integration-test stop true -Dspring.application.admin.enabled=true org.springdoc springdoc-openapi-maven-plugin 1.3 integration-test generate ${project.build.directory}/springdoc
micobarac commented 2 years ago

I have the exact same problem. Adding permission to the security configuration didn't solve the problem.

Btw, why is this issue closed?

randyhbh commented 2 years ago

@micobarac Could you share your project or a newly created one that reproduce the issue so I can take a look and see if I can help you?

micobarac commented 2 years ago

I have almost the same configuration as @RandyHBH .

When I run

mvn -Pprod,war clean verify -DskipTests=true

I get:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:start (pre-integration-test) on project openprovider-cdb: Could not figure out if the application has started: Failed to connect to MBean server at port 9001: Could not invoke shutdown operation: Connection refused to host: 127.0.0.1; nested exception is: 
[ERROR]         java.net.ConnectException: Connection refused (Connection refused)
[ERROR] -> [Help 1]
[ERROR] 
micobarac commented 2 years ago

The huge question is: Must the application be app and running on port 8080 prior to running the command above?

micobarac commented 2 years ago

Here is the significant part of pom.xml:

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                    <fork>true</fork>
                    <jvmArguments>-Dspring.application.admin.enabled=true</jvmArguments>
                    <!--
                    Enable the line below to have remote debugging of your application on port 5005
                    <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
                    -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <apiDocsUrl>http://localhost:8080/v3/api-docs</apiDocsUrl>
                    <outputFileName>openapi.json</outputFileName>
                    <outputDir>${project.basedir}/target/docs/asciidoc</outputDir>
                </configuration>
            </plugin>

I merged the existing spring-boot-maven-plugin settings with the one provided in your docs.

micobarac commented 2 years ago

I made it work by removing:

                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>

and running the app on 8080, priror to running maven mvn -Pprod,war clean verify -DskipTests=true command.

ls-pavel-makhov commented 2 years ago

Had the same problem - the issue was in the custom path, which was setup in application.yml.

pwils025 commented 1 year ago

The huge question is: Must the application be app and running on port 8080 prior to running the command above?

This plugin only works if the app server is running before it can make the output file. In spring boot, we run tests before even building the artifact. How can I just generate the file without having to run the app server?

So I just read through your source code and you're scraping the URL to parse the json and then saving it to a file. This will not work for my automation needs where the file needs to be created at build, not deploy. The phase of Integration test is misleading.

URL urlForGetRequest = new URL(apiDocsUrl);

bnasslahsen commented 1 year ago

No way for that