【问题标题】:Where and How To Define An Application Property? - JHIpster在哪里以及如何定义应用程序属性? - JHIpster
【发布时间】:2021-03-07 20:33:56
【问题描述】:

在 Spring Boot 中,可以在 application.properties 文件中定义应用程序属性。例如,Rest 的前缀可以定义为

spring.data.rest.basePath=api

对于基于 Spring Boot 的 JHipster,我猜想可以在 application.yml 文件中定义应用程序属性。但是以下方法都不适合我:404 错误。

spring.data.rest.basePath: api

spring:
    data:
        rest:
            basePath: api

另一种可能性是配置本身不起作用。

【问题讨论】:

  • 你好。您是否找到在 jHipster spring boot 中设置基本路径的解决方案?我也面临同样的问题。

标签: jhipster


【解决方案1】:

我有同样的问题,终于解决了!

引自 Jhipster 网站:

您生成的应用程序也可以有自己的 Spring Boot 属性。强烈建议这样做,因为它允许应用程序的类型安全配置,以及 IDE 中的自动完成和文档。

JHipster 在 config 包中生成了一个 ApplicationProperties 类,该类已经预先配置,并且已经在 application.yml、application-dev.yml 和 application-prod.yml 文件的底部记录。您需要做的就是编写自己的特定属性。

就我而言,我在 applicaiton-prod.yml 中设置了属性

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

在 ApplicationProperties 类中:

@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;
            }
        }

    }
}

然后我将它用作:

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

例如使用max-waithost

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

希望对你有帮助。

【讨论】:

  • 我正在尝试在应该为上传路径提供一些配置的服务中使用它。当我在服务中使用private final ApplicationProperties.Redis redis; public RedisConfiguration(ApplicationProperties applicationProperties){ redis = applicationProperties.getRedis(); } 时,我收到错误viariable可能没有配置。因为我猜这是最终结果。
【解决方案2】:

application.yml 应该是空格而不是制表符。

试试这样:

spring:
    data:
        rest:
            basePath: api

在我的应用程序中,文件位于路径中:

src\main\resources\config\application.yml

【讨论】:

  • 你的意思是另一行下面的一行应该以空格开头,而不是制表符? application.yml 中生成的行看起来像 pre ,对我来说多了一个制表符空间。
  • 我正在处理另一个问题,并将回去测试这个问题。感谢您的意见。
  • 是什么让你认为 OP 使用制表符而不是空格?
  • 我注意到 application.yml 文件中有多少空间。
  • 我试图在新行前多加一个空格,在新行前多加四个空格,但没有任何运气。如果您的意思是该位置是application.yml文件的目录,我只使用目录src\main\resources\config\application.yml中的文件。
【解决方案3】:

在尝试了数十次如何处理这个问题之后,我终于想出了如何让它发挥作用。也许它会对某人有用。

要为控制器使用前缀(例如jh),我们需要使用server.servlet.context-path 而不是spring.data.rest.basePath

链接到文档https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

所以application.yml 应该是这样的:

server:
    servlet:
        context-path: /jh
        session:
            cookie:
                http-only: true

【讨论】:

    【解决方案4】:

    此链接中的信息也有助于解决问题。

    https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-external-config.html

    根据 Haifeng Zhang 上面的回答构建应用程序配置类后,您可以通过以下方式在任何地方访问这些属性:

    @自动连线 私有 ApplicationProperties redis;

    然后访问属性

    int myEnvironmentPort = redis.getPort();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2012-03-06
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 2016-11-16
      • 2017-08-09
      • 1970-01-01
      相关资源
      最近更新 更多