小编Mar*_*ijk的帖子

手动添加约束违规

是否可以手动添加约束违规?

例如:

// validate customer (using validation annotations)
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);

if (someSpecialCase) {
    violations.add(..) 
}
Run Code Online (Sandbox Code Playgroud)

问题是该add方法接受ConstraintViolation接口,但javax.validation包中不包含可以使用的实现器.

任何的想法?

java bean-validation

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

如何确保Spring Boot额外的Jackson模块具有相同的版本?

Spring Boot已经包含核心杰克逊依赖+其他几个.

如果您想要添加org.json或jsr-353数据绑定模块,则可以明确定义这些附加模块的版本.

有没有办法引用其他Jackson模块的相同版本?我想避免任何兼容性问题.

jackson jackson-modules spring-boot

7
推荐指数
2
解决办法
8267
查看次数

如何根据正在执行的目标激活配置文件

当我从命令行执行某些目标时,我想"自动"激活一个配置文件.

比如我现在在做什么:

mvn appengine:devserver -Pdevelopment

mvn appengine:update -Pproduction
Run Code Online (Sandbox Code Playgroud)

基本上我想在运行devserver目标时自动激活开发配置文件.我在运行更新目标时要激活的生产配置文件相同(除非使用-P选项明确覆盖).

这可能吗?

google-app-engine maven-3 maven

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

Google App Engine数据存储集成测试

我想为我的Google App Engine REST服务器编写一些集成测试.

我目前的设置是我针对本地运行的GAE环境运行Rest-Assured集成测试.这对于返回json的简单调用完美无瑕.

我想更进一步,在运行Rest-Assured IT之前,我想清除数据存储区并预先填充一些测试数据.

是否可以与同一数据存储区(从集成测试)进行交互,然后本地应用程序正在运行?注意我正在运行Eclipse或mvn测试的测试.

我已经尝试过LocalServiceTestHelper

private final LocalServiceTestHelper helper =
    new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
Run Code Online (Sandbox Code Playgroud)

但这似乎并没有连接到同一个数据存储区,这也是有意义的,因为它是用于单元测试.

java google-app-engine integration-testing google-cloud-datastore

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

将@Component添加到自定义Spring Security过滤器的含义是什么?

我有一个自定义的Spring Security过滤器,扩展了GenericFilterBean.

为了进行自动依赖和bean创建,我添加了一个@Component注释.

在我的安全配置中,我也注册过滤器,如:

@Autowired
private RestAuthenticationFilter restAuthenticationFilter;

protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .addFilterBefore(restAuthenticationFilter, LogoutFilter.class)
Run Code Online (Sandbox Code Playgroud)

一切都运行良好,除了我的过滤器被调用两次......似乎Spring也自动将过滤器添加到标准过滤器.

这里最好的方法是什么?

UPDATE

@Dave这是你的意思吗?它似乎工作.

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {

    @Autowired
    private RestAuthenticationFilter restAuthenticationFilter;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ApplicationSecurity applicationSecurity() {
        return new ApplicationSecurity();
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setEnabled(false);
        filterRegistrationBean.setFilter(restAuthenticationFilter);
        return filterRegistrationBean;
    }

    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

        @Autowired
        private …
Run Code Online (Sandbox Code Playgroud)

spring spring-security-ldap spring-boot

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

有没有一种简单的方法来使用不同的Spring版本?

我想在Spring Boot项目中使用最新的Spring 4.1.x快照.

有一种简单的方法来覆盖所有Spring依赖项的版本,还是应该手动包含所有必需的Spring依赖项及其所需的版本?

原因是我想在REST服务中试验Spring 4.1 @JsonView注释.

spring spring-boot

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

Spring HATEOAS/MockMvc/JsonPath最佳实践

我正在使用MockMvc和JsonPath为Spring HATEOAS后端编写单元测试.要测试响应中包含的链接,我正在执行以下操作:

@Test
public void testListEmpty() throws Exception {
    mockMvc.perform(get("/rest/customers"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andExpect(jsonPath("$.links", hasSize(1))) // make sure links only contains self link
            .andExpect(jsonPath("$.links[?(@.rel=='self')]", hasSize(1))) //  make sure the self link exists 1 time
            .andExpect(jsonPath("$.links[?(@.rel=='self')].href", contains("http://localhost/rest/customers{?page,size,sort}"))) // test self link is correct
            .andExpect(jsonPath("$.links[?(@.rel=='self')][0].href", is("http://localhost/rest/customers{?page,size,sort}"))) // alternative to test self link is correct
            .andExpect(jsonPath("$.content", hasSize(0))); // make sure no content elements exists
}
Run Code Online (Sandbox Code Playgroud)

但是我想知道是否有一些最好的做法我应该用它来让自己更容易:

  • 测试链接包含http://localhost感觉不对.我可以使用一些Spring MovkMvc助手来确定主机吗?
  • 使用JsonPath时,很难测试数组是否包含2个属性具有特定值的元素.就像那样,数组应该包含一个具有特定值的自链接.有没有更好的方法来测试上面的内容当测试带有错误消息的字段的验证错误时,这也会发挥作用.

我在一些博文中看到了如下所示的技巧:

.andExpect(jsonPath("$.fieldErrors[*].path", containsInAnyOrder("title", "description")))
.andExpect(jsonPath("$.fieldErrors[*].message", containsInAnyOrder(
    "The maximum length of the description is …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-data-rest spring-hateoas

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

覆盖FILE_LOG_PATTERN(如果可能,每个env)

我想覆盖Spring Boot的默认文件和控制台日志模式以包含一些自定义MDC字段.

有没有一种简单的方法可以改变它application.properties/yaml?如果不是这将是一个很好的功能:-)

否则我可能要复制Boot /src/main/resources/org/springframework/boot/logging/logback base.xmlbasic.xml文件.

我不认为logback支持包含那些文件和覆盖属性值,或者它是否支持?

logback spring-boot

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

在Gradle processResources期间扩展application.yml会产生MissingPropertyException

要替换Spring Boot中的属性,我application.yml添加了:

processResources {
    filesMatching("**/application.yml") {
        expand(project.properties)
    }
}
Run Code Online (Sandbox Code Playgroud)

更换失败但给出了MissingPropertyException:

Caused by: groovy.lang.MissingPropertyException: No such property: OPENSHIFT_MYSQL_DB_HOST for class: SimpleTemplateScript1
        at SimpleTemplateScript1.run(SimpleTemplateScript1.groovy:49)
        at org.gradle.api.internal.file.copy.FilterChain$3.transform(FilterChain.java:95)
        at org.gradle.api.internal.file.copy.FilterChain$3.transform(FilterChain.java:84)
        at org.gradle.api.internal.ChainingTransformer.transform(ChainingTransformer.java:37)
        at org.gradle.api.internal.file.copy.FilterChain.transform(FilterChain.java:39)
        at org.gradle.api.internal.file.copy.FilterChain.transform(FilterChain.java:46)
        at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.open(DefaultFileCopyDetails.java:86)
        at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:56)
        at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.copyTo(DefaultFileCopyDetails.java:94)
        at org.gradle.api.internal.file.AbstractFileTreeElement.copyFile(AbstractFileTreeElement.java:93)
        at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:74)
        ... 81 more
Run Code Online (Sandbox Code Playgroud)

最初我的application.yml包含:

url: jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}
Run Code Online (Sandbox Code Playgroud)

请注意,这些Openshift变量仅在Openshift生产环境中知道,但在dev模式下本地运行时则不知道.

http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html所述:You can also include arbitrary Groovy code in the file, such as ${version ?: 'unknown'}所以我改为application.yml:

url: jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST ?: ''}:${OPENSHIFT_MYSQL_DB_PORT …
Run Code Online (Sandbox Code Playgroud)

gradle spring-boot

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

如何使用 Spring webflux 实现/迁移 OncePerRequestFilter

使用 Spring web 可以简单地OncePerRequestFilter(见下文)维护请求跨度的请求 id。将生成的请求 ID 存储在请求属性中,将其添加到日志记录 MDC,并在响应标头中返回。

我知道反应式 webflux 堆栈完全不同,那么应该如何解决这个问题呢?

我找到了https://github.com/spring-projects/spring-framework/issues/20239但不清楚现在支持或不支持什么。

@Component
public class RequestIdFilter extends OncePerRequestFilter implements Ordered {

    private static final String MDC_KEY = "requestId";
    private static final String REQUEST_ATTRIBUTE_NAME = "requestId";
    private static final String RESPONSE_HEADER_NAME = "X-Request-Id";


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        var requestId = UUID.randomUUID().toString();
        MDC.put(MDC_KEY, requestId);
        request.setAttribute(REQUEST_ATTRIBUTE_NAME, requestId);
        response.setHeader(RESPONSE_HEADER_NAME, requestId);
        try {
            filterChain.doFilter(request, response);
        } finally {
            MDC.remove(MDC_KEY);
        }
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-webflux

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