Where and How To Define An Application Property? - JHIpster

后端 未结 3 782
终归单人心
终归单人心 2021-01-19 01:41

In Spring Boot, an application property can be defined in application.properties file. For example, a prefix of Rest can be defined as

spring.data.rest.baseP         


        
相关标签:
3条回答
  • 2021-01-19 01:52

    After trying dozens of time how to handle the problem I finally figured out how to make it work. Maybe it will be useful for somebody.

    To use prefix for controllers (let's say for example jh) we need to use server.servlet.context-path and not spring.data.rest.basePath.

    Link to documentation https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

    So application.yml should looks like this:

    server:
        servlet:
            context-path: /jh
            session:
                cookie:
                    http-only: true
    
    0 讨论(0)
  • 2021-01-19 02:05

    I have same problem and finally figured it out!

    Quote from Jhipster website:

    Your generated application can also have its own Spring Boot properties. This is highly recommended, as it allows type-safe configuration of the application, as well as auto-completion and documentation within an IDE.

    JHipster has generated a ApplicationProperties class in the config package, which is already preconfigured, and it is already documented at the bottom the application.yml, application-dev.yml and application-prod.yml files. All you need to do is code your own specific properties.

    In my case, I have set the properties in applicaiton-prod.yml

    application:
        redis:
            host: vnode1
            pool:
                max-active: 8
                max-idle: 8
                max-wait: -1
                min-idle: 0
            port: 6379
    

    In ApplicationProperties class:

    @ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
    public class ApplicationProperties {
    
        public final Redis redis = new Redis();
    
        public Redis getRedis() {
            return redis;
        }
    
        public static class Redis {
    
            private String host = "127.0.0.1";
    
            private int port = 0;
    
            public String getHost() {
                return host;
            }
    
            public void setHost(String host) {
                this.host = host;
            }
    
            public int getPort() {
                return port;
            }
    
            public void setPort(int port) {
                this.port = port;
            }
    
            private Pool pool = new Pool();
    
            public void setPool(Pool pool) {
                this.pool = pool;
            }
    
            public Pool getPool() {
                return this.pool;
            }
    
            public static class Pool {
                private int maxActive = 8;
                private int maxWait = -1;
    
                public int getMaxIdle() {
                    return maxIdle;
                }
    
                public void setMaxIdle(int maxIdle) {
                    this.maxIdle = maxIdle;
                }
    
                private int maxIdle = 8;
                private int minIdle = 0;
    
    
                public void setMaxActive(int maxActive) {
                    this.maxActive = maxActive;
                }
    
                public int getMaxActive() {
                    return maxActive;
                }
    
                public int getMinIdle() {
                    return minIdle;
                }
    
                public void setMinIdle(int minIdle) {
                    this.minIdle = minIdle;
                }
    
                public int getMaxWait() {
                    return maxWait;
                }
    
                public void setMaxWait(int maxWait) {
                    this.maxWait = maxWait;
                }
            }
    
        }
    }
    

    Then I use it as:

    private final ApplicationProperties.Redis redis;
    public RedisConfiguration(ApplicationProperties applicationProperties){
        redis = applicationProperties.getRedis();
    }
    

    For instance use max-wait and host:

    this.redis.getPool().getMaxWait();
    this.redis.getHost();
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-19 02:13

    The application.yml should be whit spaces not tabs.

    Try like this:

    spring:
        data:
            rest:
                basePath: api
    

    In my application the file is in the path:

    src\main\resources\config\application.yml
    
    0 讨论(0)
提交回复
热议问题