Home > OS >  yml config about http and https
yml config about http and https

Time:11-17

In application.yml, I need to config https port and http port.

For this, code is as follows in application.properties.

server.port.http=80
server.port.https=443

In application.yml, I tried follwing code for configuration,
but it doesn't work.

code1

server:
  port: 443
    http: 80

code2

server:
  port:
    http: 80
  port: 443

How do this only with application.yml?
I can do it with collaboration of .properties and .yml.
However, I want to do this with only application.yml.


Solution

I found solution thanks to answer of dariosicily.

I configured https and http port as follows.

server:
  port:
    https: 443
    http: 8080

In additional, to configure spring boot default port, I've added code as follows.

package refill.station;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collections;

@SpringBootApplication
public class StationApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(StationApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "443"));
        app.run(args);
    }

}

It works really well. Thank you!!!

CodePudding user response:

You can configure both http and https ports creating a dictionary with http and https keys in your application.yml :

server:
  port: 
      http: 8080
      https: 443

Then you can access both properties in your java class (or kotlin with an equivalent code) :

@Value("${server.port.http}")
private int httpPort;
    
@Value("${server.port.https}")
private int httpsPort;
  • Related