小编Jel*_*tor的帖子

在Spring Boot应用程序中对@Value注释字段强制实施约束

我有以下字段注释@Value,指定一个默认值:

@Value("${tolerance.percentage:25}")
private int tolerance;
Run Code Online (Sandbox Code Playgroud)

如果该支柱存在,该代码会正确地将字段的值初始化为系统属性"tolerance.percentage".如果它不存在,则默认为25.

我想更进一步,通过在这个int字段强制执行min和max,因为它代表一个小于100的百分比作为一个整数,而Murphy定律意味着有人(可能是我)可以从外部错误配置属性和我的应用程序会在运行时开始做奇怪的事情,这对我来说太迟了.如果在应用程序启动时将属性设置为"101"或"-1",我想要抛出一个错误.哎呀,如果我试着在@Value注释中将它默认为101,我甚至会喜欢抛出错误,但这对于这个问题的目的并不重要.这是我试过的:

//@Min and @Max don't produce the intended behavior when combined with @Value
@Min(0)
@Max(100)
@Value("${tolerance.percentage:25}")
private int tolerance;
Run Code Online (Sandbox Code Playgroud)

我可以在知道的int字段上强制执行最小值和最大值@Value吗?

java spring spring-boot

9
推荐指数
1
解决办法
5282
查看次数

获取可变深度数组中的第一个数字数组

我在JavaScript框架中使用一个函数,其中返回值可以是以下任何一个

  1. 一个xy坐标对

    [x,y]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 一组xy坐标对

    [[x,y],[x,y],...]
    
    Run Code Online (Sandbox Code Playgroud)
  3. xy坐标对的数组数组

    [[[x,y],[x,y]],[[x,y],[x,y]],...]
    
    Run Code Online (Sandbox Code Playgroud)

返回值取决于对象的几何形状(单点,线或多行).无论返回值及其数组深度如何,我都想获取第一个xy坐标对.有效的方法是什么?

这是我到目前为止实现目标的代码:

//here is the magic method that can return one of three things :)
var mysteryCoordinates = geometry.getCoordinates();
var firstCoord;

if(typeof mysteryCoordinates[0] === 'number') {
    firstCoord = mysteryCoordinates;
} else if (typeof mysteryCoordinates[0][0] === 'number') {
    firstCoord = mysteryCoordinates[0];
} else if (typeof mysteryCoordinates[0][0][0] === 'number') {
    firstCoord = mysteryCoordinates[0][0];
}
Run Code Online (Sandbox Code Playgroud)

我真的很讨厌这个解决方案,我正在寻找更优雅的东西.

javascript arrays multidimensional-array

7
推荐指数
1
解决办法
113
查看次数

Tomcat 中是否可以使用 https 进行 gzip 压缩?

是否可以为 https 配置 Tomcat (7.0.75) 并同时启用 gzip 压缩?我无法找出 http 连接器属性的有效组合server.xml来完成工作。在 http 上工作的四个与压缩相关的属性:

compression="on"
compressableMimeType="application/json"
compressionMinSize="8192"
useSendfile="false"
Run Code Online (Sandbox Code Playgroud)

配置 https 时似乎没有任何影响。我目前的配置:

compression="on"
compressableMimeType="application/json"
compressionMinSize="8192"
useSendfile="false"
Run Code Online (Sandbox Code Playgroud)

compression https tomcat gzip

5
推荐指数
1
解决办法
3038
查看次数

使用条件元素定义JavaScript数组文字

我想使用包含元素的文字来创建JavaScript数组。如果某个表达式为真,我只希望包含这些元素的一部分(在数组中间的某个位置)。我显然可以使用始终存在的元素创建数组,然后在条件为真的情况下以编程方式在适当的索引处插入其他元素,但是我不想这样做,因为非ES6的处理方式不是很如果条件为真(不是很易读),那么您就必须认真思考索引,以了解条件元素将要去往何处。这是我知道该怎么做(但不喜欢)与我想做什么(但不知道怎么做)的简化示例。在最后一个示例中,undefined在索引中,我根本不希望有元素。有没有一种方法可以通过文字和表达式来实现,或者我将不得不做一些数组操作来实现这一点?

function createArrayTheWayIDislike(condition) {
    var array = [
        'a',
        'd'
    ];
    if(condition) {
       array.splice.apply(array, [1, 0].concat(['b', 'c']));
    }
    console.log(array);
}

function createArrayTheWayIWantTo(condition) {
    var array = [
        'a',
        condition ? 'b' : undefined,
        condition ? 'c' : undefined,
        'd'
    ];
    console.log(array);
}

createArrayTheWayIDislike(true);
createArrayTheWayIDislike(false);

createArrayTheWayIWantTo(true);
createArrayTheWayIWantTo(false);
Run Code Online (Sandbox Code Playgroud)

javascript arrays

1
推荐指数
1
解决办法
92
查看次数