小编Mic*_*ner的帖子

Spring Boot Test忽略logging.level

我的一个maven模块在运行测试时忽略了我的日志记录级别.

在src/test/resources中我有application.properties:

app.name=bbsng-import-backend
app.description=Import Backend Module for Application
spring.profiles.active=test

# LOGGING
logging.level.root=error
logging.level.org.springframework.core =fatal
logging.level.org.springframework.beans=fatal
logging.level.org.springframework.context=fatal
logging.level.org.springframework.transaction=error
logging.level.org.springframework.test=error
logging.level.org.springframework.web=error
logging.level.org.hibernate=ERROR
Run Code Online (Sandbox Code Playgroud)

我也尝试过application-test.properties.

我的应用程序记录很多,特别是在加载上下文时.我尝试了logback.xml,logback-test.xml和logback-spring.xml,但没有任何帮助.

我的pom:

<parent>
    <groupId>at.company.bbsng</groupId>
    <artifactId>bbsng-import</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>

<properties>
    <start-class>at.company.bbsng.dataimport.ApplicationImportBackend</start-class>
</properties>


<dependencies>

    <!-- APPLICATION ... -->
    <dependency>
        <groupId>at.company.bbsng</groupId>
        <artifactId>bbsng-app-domain</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SPRING ... -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- JAVAX ... -->
       ...

    <!-- COMMONS ... -->
       ...

    <!-- LOMBOK ... -->
       ...

    <!-- DB -->
       ... …
Run Code Online (Sandbox Code Playgroud)

logging ignore logback spring-boot

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

Spring Boot MVC多模块可执行jar

我有一个多模块项目,使用弹簧启动1.1.7构建

结构是

+ parent
    + import
        + web
        + backend
Run Code Online (Sandbox Code Playgroud)

我的父模块将包括一些微服务,我想从我父母管理的东西(所有使用的依赖项)等等.在导入/后端有我的批处理业务逻辑,在web中有一个mvc应用程序,从那里我可以开始批处理作业.

在Eclipse中,一切正常,我可以从Application.java文件和应用程序启动应用程序.

现在我想通过执行可执行jar文件来执行该应用程序,但是当我尝试从控制台启动时,我收到以下错误消息.

java -jar application.jar
Kein Hauptmanifestattribut in application.jar
Run Code Online (Sandbox Code Playgroud)

jar是非常小的只有5kb,我没有在jar包中找到任何3方依赖的罐子.

Web模块的Pom如下:

    <project>

        <modelVersion>4.0.0</modelVersion>

        <parent>
            <groupId>at.company.bbsng</groupId>
            <artifactId>bbsng-import</artifactId>
            <version>0.1.0-SNAPSHOT</version>
        </parent>

        <artifactId>bbsng-import-web</artifactId>
        <name>bbsng-import-web</name>
        <packaging>jar</packaging>

        <properties>
            <start-class>at.company.bbsng.dataimport.Application</start-class>
        </properties>


        <dependencies>

             <!-- APPLICATION -->
            <dependency>
                <groupId>at.company.bbsng</groupId>
                <artifactId>bbsng-import-backend</artifactId>
                <version>${parent.version}</version>
            </dependency>

            <!-- SPRING ... -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>spring-boot-starter-logging</artifactId>
                        <groupId>org.springframework.boot</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

            ...


        </dependencies>



        <build>

            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>

        </build>

</project> …
Run Code Online (Sandbox Code Playgroud)

java spring jar executable-jar spring-boot

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

消耗Spring Hateoas Pageable

我有一个使用HAteoas的Rest-Service,以前没有分页的工作.现在我正在制作可分页的Json.我用Spring-Hateoas的开箱即用功能做到了这一点.但现在我正在坚持消费它,我想它确实没有很好的记录,如果是的话.

我的JSON如下所示:

{
"_embedded": {
"vertragResourceList": [
  {
    "identifier": 728,
    "auszubildender": "Rumm",
    "beruf": "Landwirt/in",
    "betrieb": "Mitterbauer Johann",
    "betriebsNummer": "e12d0949-67ae-4134-9dc2-fb67758b6b16",
    "zustaendigeStelle": "Irgendwo",
    "beginn": 529887600000,
    "status": "RECENT",
    "fachrichtung": null,
    "schwerpunkt": "Grünland oder Ackergras",
    "ende": 623113200000,
    "_links": {
      "self": {
        "href": "http://localhost:8080/bbsng-app-rest/vertrag/728"
      }
    }
  },
  {
    "identifier": 803,
    "auszubildender": "Gossen",
    "beruf": "Landwirt/in",
    "betrieb": "Beer Johann",
    "betriebsNummer": "d5a20cb9-7273-4b75-85bd-f8e7d6a843c4",
    "zustaendigeStelle": "Woanders",
    "beginn": 278118000000,
    "status": "RECENT",
    "fachrichtung": null,
    "schwerpunkt": "Ackerfutterbau",
    "ende": 339116400000,
    "_links": {
      "self": {
        "href": "http://localhost:8080/bbsng-app-rest/vertrag/803"
      }
    }
  }
]
},
"page": {
"size": …
Run Code Online (Sandbox Code Playgroud)

java rest hateoas consumer spring-hateoas

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

使用RestTemplate消耗Spring Hateoas Restservice

我有两个应用程序,一个叫做bar,它为我提供了HAL格式的资源.另一个是bcm来使用该服务.

响应栏的示例如下所示:

[
    {
        "name":"Brenner/in",
        "_links":{
            "self":{
                "href":"..host/bbsng-app-rest/betrieb/15"
            }
        }
    },
    {
        "name":"Dienstleistungshelfer/in HW",
        "_links":{
            "self":{
                "href":"..host/bbsng-app-rest/betrieb/4"
            }
        }
    },
    {
        ...
Run Code Online (Sandbox Code Playgroud)

现在我尝试使用Spring RestTemplate 从bcm中消耗它.我的解决方案有效,但我不满意这个解决方案,我想有一个更干净的方式.

我的客户端代码使用RestService看起来像:

@Autowired private RestTemplate template;

@Override
@SuppressWarnings("unchecked")
public BerufListe findeAlleBerufe() {
    final BerufListe berufListe = new BerufListe();

    final ResponseEntity<List> entity = template.getForEntity(LinkUtils.findBeruf(), List.class);

    if (OK.equals(entity.getStatusCode())) {
        final List<LinkedHashMap> body = entity.getBody();
        for (final LinkedHashMap map : body) {
            final LinkedHashMap idMap = (LinkedHashMap) map.get("_links");
            String id = …
Run Code Online (Sandbox Code Playgroud)

java client hal resttemplate spring-hateoas

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

使用Spring Boot和Thymeleaf加载静态资源

我想加载我的静态资源.首先我认为它已经有效,但这只是浏览器缓存的一个技巧.我只按预期加载了html文件,但我没有得到js,css,图像等等.

======

我的StartClass:

@Configuration
@Import({ ServiceConfig.class, WebMvcConfig.class })
@EnableHypermediaSupport(type = HAL)
@EnableAutoConfiguration
public class ApplicationClientMvc {
    public static void main(final String[] args) {
        SpringApplication.run(ApplicationClientMvc.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

======

WebMvcConfig

@Configuration
@ComponentScan
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired public SpringTemplateEngine templateEngine;

    @Bean
    public ThymeleafTilesConfigurer tilesConfigurer() {
        final ThymeleafTilesConfigurer configurer = new ThymeleafTilesConfigurer();
        configurer.setDefinitions("classpath*:/templates/**/views.xml");
        return configurer;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        final ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setViewClass(ThymeleafTilesView.class);
        resolver.setTemplateEngine(templateEngine);
        resolver.setCharacterEncoding(UTF_8);
        return resolver;
    }

    @Bean
    public TilesDialect tilesDialect() {
        return new …
Run Code Online (Sandbox Code Playgroud)

configuration static-resource thymeleaf spring-boot

6
推荐指数
2
解决办法
2万
查看次数

Gradle 多模块项目无法解析依赖子模块的类

该应用程序在 Eclipse 中运行良好。但我无法通过 gradle ( gradle clean build)构建应用程序

结构如下:

financial-calculator(家长)

  • library
  • api

api想要使用的类library

我父母的settings.gradle档案:

rootProject.name = 'financial-calculator'

include 'library'
include 'api'  

rootProject.children.each { it.name = rootProject.name + '-' + it.name }
Run Code Online (Sandbox Code Playgroud)

build.gradle文件:

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "o.s.b:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

allprojects {
    apply plugin: 'eclipse'
    apply plugin: 'maven'
    apply plugin: 'maven-publish'

    group = 'net.hemisoft.financial.calculator'
    version = "0.1.0-SNAPSHOT"
}

subprojects {
    apply …
Run Code Online (Sandbox Code Playgroud)

groovy dependencies gradle multi-module

6
推荐指数
0
解决办法
469
查看次数

如何使用 javax.validation.constraints.Pattern 允许特定单词?

我想使用 javax.validation.constraints.Pattern 进行验证。文本不应是“无”或“其他”。

允许的示例:

  • 某物
  • 单词

不允许:

  • 没有任何
  • 其他的

我正在尝试,但我没有意识到我的问题。就像是:

@NotNull
@Pattern(regexp = "(^none)")
String  countryValue;
Run Code Online (Sandbox Code Playgroud)

谢谢你的提示。

更新: 正如 Anish 所说的在线正则表达式验证器,正则表达式^(?!others|none)应该是正确的。但Spring-MVC仍然否认。是否需要使用特殊的语法?我给出更多代码以获得更大的图景:

控制器:

@PostMapping
public String post(@ModelAttribute @Valid DisclaimerFormDto disclaimerForm, BindingResult errors, ModelMap modelMap) {
    if(errors.hasErrors()) {
        errors.getAllErrors().forEach(System.out::println);
        return "redirect:/disclaimer";
    }
    return "redirect:/product";
}
Run Code Online (Sandbox Code Playgroud)

FormDto(包含 Anish 提到的更改):

@Data
@ToString
public class DisclaimerFormDto {
    @NotNull
    @Pattern(regexp = "^(?!others|none)")
    String  countryValue;
}
Run Code Online (Sandbox Code Playgroud)

绑定结果的输出:

Field error in object 'disclaimerFormDto' on field 'countryValue': rejected value [none]; codes [Pattern.disclaimerFormDto.countryValue,Pattern.countryValue,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [disclaimerFormDto.countryValue,countryValue]; arguments …
Run Code Online (Sandbox Code Playgroud)

spring jpa spring-mvc

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

使用可执行Jar时找不到Spring-Boot资源

我再次遇到一个奇怪的问题,希望有人可以提供帮助.

我有一个spring boot后端模块,在Eclipse中启动main时,应用程序是可执行的.一切都很好.

我的应用程序使用ss/main/resources文件夹中包含的csv文件将示例数据导入数据库.如上所述,在eclipse中启动时一切正常.

现在我想将它作为可执行jar执行,应用程序开始启动然后无法启动,因为它无法找到csv文件.它打印出来的路径,它在哪里查找文件,是正确的,并且csv文件包含在jar中.

该模块的Pom如下所示:

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>at.company.bbsng</groupId>
        <artifactId>bbsng-import</artifactId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>

    <artifactId>bbsng-import-backend</artifactId>
    <name>bbsng-import-backend</name>

    <properties>
        <start-class>at.company.bbsng.dataimport.Application</start-class>
    </properties>


    <dependencies>

        <!-- SPRING ... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <!-- EXCLUDE LOGBACK AND USE LOG4J -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- COMMONS ... -->

        ...

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

csv文件的路径在propery文件中配置如下:

# EXAMPLE PATH
csv.path=config/csv/
Run Code Online (Sandbox Code Playgroud)

java配置文件的部分如下:

  ...

  @Value("${csv.path}")
  private …
Run Code Online (Sandbox Code Playgroud)

java resources filenotfoundexception executable-jar spring-boot

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

AssertJ 比较双精度值

我想将双精度值与 AssertJ 进行比较。我不明白为什么我的测试失败。

@Test
public void testInterestToQuote() {
    double result = BasicCalculator.accumulationFactorByYearsAndInterest(years, interest)
    Assertions.assertThat(result).isCloseTo(expected, Assertions.offset(0.1d))
}
Run Code Online (Sandbox Code Playgroud)

例外是:

java.lang.AssertionError: 
Expecting:
   <7.256571590148141E-5>
to be close to:
   <7.25>
by less than <0.1> but difference was <7.249927434284099>.
(a difference of exactly <0.1> being considered valid)
Run Code Online (Sandbox Code Playgroud)

为什么断言失败?

double compare offset assertj

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

带有 Bucket4j 的 Spring Boot 2 以防止 DDOS 攻击

我想防止对我的 spring boot 2 web 应用程序的 DDOS 攻击,我遇到了框架bucket4j( bucket4j-github )。我的应用程序将在 heroku 上运行,他们建议这样做(Heroku-Link

到现在为止还挺好。我现在正在尝试实施bucket4j,我希望在那之后,如果我F5非常快速和频繁地按下,就会发生一些事情,会发生错误或类似的事情。但是它的行为就像没有bucket4j框架一样,请求已全部得到答复。

我的目标是限制来自 ip 的速率。目前我只是从本地主机尝试过。

到目前为止我所拥有的:

pom.xml

    <dependency>
        <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
        <artifactId>bucket4j-spring-boot-starter</artifactId>
        <version>0.1.15</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    <dependency><!-- Somehow I needed that since bucket4j--> 
        <groupId>javax.interceptor</groupId>
        <artifactId>javax.interceptor-api</artifactId>
        <version>1.2.2</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

应用程序属性

# CACHE
spring.cache.jcache.config=classpath:ehcache.xml

# RATE LIMIT
bucket4j.enabled=true
bucket4j.filters[0].cache-name=buckets
bucket4j.filters[0].filter-method=servlet
bucket4j.filters[0].url=/* 
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=10
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=1
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=minutes
bucket4j.filters[0].rate-limits[0].expression=getRemoteAddress()
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval=0
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval-unit=minutes
Run Code Online (Sandbox Code Playgroud)

缓存文件

<config ... >
    <cache alias="buckets">
        <expiry><ttl unit="seconds">3600</ttl></expiry>
        <heap unit="entries">1000000</heap>
    </cache> …
Run Code Online (Sandbox Code Playgroud)

ddos rate-limiting spring-boot

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