小编use*_*453的帖子

proxy_pass 基于标头而不使用 if 条件

我正在寻找一种方法,将所有在 HTTP 标头中设置了Account-ID最后两位数字(十位数字)的account-id 的请求重新00路由到05(仅触及总流量的 5%)。此外,如果请求设置了 HTTP header Server-A,则无论设置的 account-id 是什么,该请求都应该转发到该服务器。否则,默认情况下,所有流量都应由 server-b 处理。我当前的位置规则如下所示:

location /rest/accounts/ {
  if ( $http_account_id ~ '([\d]{8})(0[0-4])') {
    proxy_pass http://server-a.company.com;
  }

  if ( $http_server_a = true) {
    proxy_pass http://server-a.company.com;
  }

  proxy_pass http://server-b.company.com;
}
Run Code Online (Sandbox Code Playgroud)

正如我在官方文档中看到的,这里if被认为是邪恶的

对于我的方法是否有更好的解决方案,而不被认为是邪恶的

nginx nginx-location

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

自定义适配器中的黄油刀视图持有人模式

我正在尝试在自定义适配器中使用Butter Knife,如文档广告:http://jakewharton.github.io/butterknife/ 我粘贴了示例代码并使用我的RelativeLayout文件插入布局:

  @Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    ButterKnife.setDebug(true);
    if (view != null) {
      holder = (ViewHolder) view.getTag();
    } else {
      view = inflater.inflate(R.layout.list_row_people, parent, false);
      holder = new ViewHolder(view);
      view.setTag(holder);
    }

    holder.name.setText("John Doe");
    // etc...

    return view;
  }
Run Code Online (Sandbox Code Playgroud)

这是ViewHolder的代码(内联类PeopleAdapter):

static class ViewHolder {
    @BindView(R.id.name)
    TextView name;

    public ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,holder.name.setText("John Doe");抛出NullPointer异常为namenull.调试输出显示以下行:

13:11:32.816 11613-11613/com.myproject.debug D/ButterKnife: Looking up …
Run Code Online (Sandbox Code Playgroud)

android butterknife android-viewholder

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

在根项目中找不到路径为“build”的任务

我有一个多项目,在构建最后一个子项目后,我想处理所有 jars。因此我在根项目中创建了一个任务:

task install(dependsOn: 'build', type: Copy) {
    doLast {
        println "exec install task"
    }
}
Run Code Online (Sandbox Code Playgroud)

./gradlew install根目录中调用时,我遇到了这个错误:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':install'.
> Task with path 'build' not found in root project 'foo'.
Run Code Online (Sandbox Code Playgroud)

但是,呼叫./gradlew tasks向我展示了这些任务:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project. …
Run Code Online (Sandbox Code Playgroud)

gradle build.gradle

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

在 Gradle 中设置 JAVA_OPTS 和 JAVA_TOOL_OPTIONS

运行大型 Gradle 构建(使用 JDK7)我收到两个OutOfMemoryErrors

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Test worker" 
Run Code Online (Sandbox Code Playgroud)

当我设置下面的两个环境变量时,构建运行并正常工作:

export JAVA_OPTS="-Xmx2048m -XX:MaxPermSize=1024m"
export JAVA_TOOL_OPTIONS="-Xmx1024m -XX:MaxPermSize=1024m -Xms768m"
./gradlew test --stacktrace
...
Picked up JAVA_TOOL_OPTIONS: -Xmx1024m -XX:MaxPermSize=1024m -Xms768m
...
Run Code Online (Sandbox Code Playgroud)

有没有办法将这些设置包含在gradle.properties或 中build.gradle?如果是,正确的用法是什么?

我已经在build.gradle

allprojects {
    System.setProperty('JAVA_OPTS', "-Xmx2048m -XX:MaxPermSize=1024m")
    System.setProperty('JAVA_TOOL_OPTIONS', "-Xmx1024m -XX:MaxPermSize=1024m -Xms768m")
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

gradle java-opts build.gradle

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

使用 SpringRunner 加快 SpringBootTest 的启动时间

我正在寻找一种方法来最小化 a 的启动时间SpringBootTest,目前启动和执行测试最多需要 15 秒。我已经使用了特定类的嘲笑webEnvironment和。standaloneSetup()RestController

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = MOCK)
public class DataControllerMvcTests {

    @Autowired
    private DataService dataService;

    @Autowired
    private DataController dataController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .standaloneSetup(dataController)
                .build();
    }

    @Test
    @WithMockUser(roles = "READ_DATA")
    public void readData() throws Exception {
        mockMvc.perform(get("/data")).andExpect(status().is2xxSuccessful());
    } …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-boot-test

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

在Kotlin中使用Singleton的StackOverflowError

任何人都可以说,为什么我要java.lang.StackOverflowError使用这个Kotlin课程?第41行是if (instance == null) {

class TokenHelper protected constructor() {
    var token: String? = null
    var appId: String? = null
    var installationId: String? = null
    var userId: String? = null

    companion object {
        var instance: TokenHelper? = null
            get() {
                if (instance == null) {
                    instance = TokenHelper()
                }
                return instance
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

04-11 19:07:42.188 16142-16142/com.foo.bar.debug E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.foo.bar.debug, PID: 16142
  java.lang.StackOverflowError: stack size 8MB
      at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:0)
      at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
      at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
      at …
Run Code Online (Sandbox Code Playgroud)

android jetbrains-ide kotlin kotlin-android-extensions

4
推荐指数
2
解决办法
1229
查看次数

Spring Data JPA:查询对象列表中的字段

我正在寻找正确的方法名称,以找到所有Document具有给定Target的的type

public class Document {

    @Indexed(name = "targets")
    private List<Target> targets;

    public class Target {
        private String value;
        private String type;
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,以下所有方法名称均无效:

List<Document> findByTargetType(...);
List<Document> findByTargetsTargetType(...);
List<Document> findByTargetsContainsTargetType(...);
List<Document> findByTargetsContainsTargetTargetType(...);
List<Document> findByTargetsContainingTargetType(...);
List<Document> findByTargetsContainingTargetTargetType(...);
Run Code Online (Sandbox Code Playgroud)

那么,实现所需功能的正确方法名称是?

spring-data spring-data-jpa spring-data-mongodb

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

使用幻数(1)或全局常量检查列表大小?

我和同事讨论以下代码:

private static final byte ONE_ELEMENT = 1;
private boolean isListSizeEqualsOne(List<MyClass> myList) {
    return myList.size() == ONE_ELEMENT;
}
Run Code Online (Sandbox Code Playgroud)

我认为这种代码无疑会减少对一个神奇数字的警告,但同时会不必要地增加混乱.我建议改为内联全局变量:

private boolean isListSizeEqualsOne(List<MyClass> myList) {
    return myList.size() == 1;
}
Run Code Online (Sandbox Code Playgroud)

有没有关于这个例子的文献?

java design-patterns

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

在application.yml中定义基本安全性用户,密码和角色

我正在寻找一种使用@SecuredSpring Boot注释保护方法的方法。对于大约10-15个用户,我不想连接到数据库并从那里获取用户及其权限/角色,而是将它们本地存储在特定于配置application.yml文件的文件中。Spring Boot中是否有一个支持该想法的概念?到目前为止,我所能找到的所有内容都可以与基本的安全执行器('org.springframework.boot:spring-boot-starter-security')一起使用,看起来像这样:

security:
  basic:
    enabled: true
  user:
    name: admin
    password: admin
    role: EXAMPLE
Run Code Online (Sandbox Code Playgroud)

但是,@RolesAllowed("READ")即使我假设用户admin不应该访问该方法,我仍然能够访问带有注释的 方法。我的SecurityConfiguration看起来像这样:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
@Profile("secure")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest()
                .fullyAuthenticated()
                .and()
                .httpBasic();

        http.sessionManagement()
                .sessionFixation()
                .newSession();

        http.csrf().disable();
        http.headers().frameOptions().disable();

    }
}
Run Code Online (Sandbox Code Playgroud)

最终这可能是一个不同的问题,但对我自己的理解也许很重要。

我想知道如何在我application.yml和注释方法中指定具有不同密码和不同角色的多个用户,以确保只有授权用户才能访问这些方法。

spring spring-security spring-boot spring-boot-actuator

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

IntelliJ 2016.3.4中突然出现java.lang.NoClassDefFoundError

昨天我的应用程序工作正常但是当我今天尝试通过IntelliJ启动它时,我只是得到这个错误:

Connected to the target VM, address: '127.0.0.1:41617', transport: 'socket'
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at com.company.app.Application.main(Application.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
Disconnected from the target VM, address: '127.0.0.1:41617', transport: 'socket'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

这是一个使用Gradle(3.4.1)构建的Spring Boot(1.5.1)应用程序.当我调用gradle bootRun应用程序开始没有麻烦.我已经尝试从头开始重新导入(删除整个目录并重新克隆它)并删除本地.gradle目录.总共有两个应用程序不再工作,但自昨天以来没有任何代码更改.

intellij-idea gradle spring-boot

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

MongoDB多久检查一次其索引“ expireAfterSeconds”?

我在本地使用一个单一实例MongoDB,并在Spring Boot存储库中,使用以下注释对文档类进行注释:

@Indexed(name = "deleteAt", expireAfterSeconds = 0)
private LocalDateTime deleteAt;
Run Code Online (Sandbox Code Playgroud)

正如我从此处的文档中读取的那样,文档应在指定的位置自动删除DateTime。它通常可以正常工作,但是当我频繁轮询同一文档时,我看到删除操作有所延迟。大多数文档被立即删除,但是在指定的删除时间之后,有些文档仍保留在数据库中长达30秒。

这让我想知道MongoDB是否运行调度程序来清理此类文档,如果可以的话,它多久运行一次?

java mongodb spring-data-mongodb

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

使用@Value在Spring Boot 2.0中访问buildInfo

以前在Spring Boot 1.x中,我编写了Gradle任务将jar的构建版本复制到应用程序的版本,application.yml并用regex替换给定的属性,例如info.build.version: 0.0.1

迁移到Spring Boot 2.0后,我意识到有一个io.spring.dependency-management插件可以让我定义buildInfo任务:

springBoot {
    buildInfo()
}
Run Code Online (Sandbox Code Playgroud)

这在访问/info执行器时有效并成功显示相同的信息。

现在我想使用生成build.versionMETA-INF/build-info.properties两个用例:

  1. 在SwaggerUI中显示版本
  2. 在每个日志行中包含版本

以前,足以访问这样的属性:@Value("${info.build.version:undefined}") String buildVersion 或in logback-spring.xml

<springProperty scope="context" name="applicationVersion" source="info.build.version"/>
Run Code Online (Sandbox Code Playgroud)

不幸的是,即使我替换info.build.versionbuild.version(我希望它能正常工作),两个访问器也不再起作用。

我相信将版本包含在logback中仅几步之遥,即通过@Value注释访问属性,因此这是我的问题的核心:

我怎样才能通过访问生成build.version的?META-INF/build-info.properties@Value

我也尝试添加任务

processResources {
    filesMatching('build-info.properties') {
        expand(project.properties)
    }    
}
Run Code Online (Sandbox Code Playgroud)

/sf/answers/2943598871/中所建议,但这似乎没有任何效果。

spring-boot spring-boot-gradle-plugin

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

W/GooglePlayServicesUtil:Google Play服务已过期.需要8298000但找到8107534

关于SO的许多其他问题,我遇到类似的错误消息.与其他问题不同,这与硬件调试设备有关.

我的gradle wear-build文件如下所示:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:1.2.0'
    compile 'com.google.android.gms:play-services-wearable:8.3.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
}
Run Code Online (Sandbox Code Playgroud)

在模拟器中调试应用程序工作正常,但是一旦我发布应用程序并将其安装在我的可穿戴设备上,移动设备和磨损之间的连接将无法正常工作.通过蓝牙调试磨损(除了快速之外的其他任何东西),显示了这一小部分调试信息:

W/GooglePlayServicesUtil: Google Play services out of date. Requires 8298000 but found 8107534

我认为gradle依赖项会将可穿戴服务打包到已发送的apk(因为有'compile'),这不是正确的吗?

如何使硬件手表工作?

android bluetooth google-play-services wear-os

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