标签: spring-boot

如何从 Java Spring Boot 应用程序调用 GraphQL API?是否有支持 GraphQL 查询形成的注释?

我正在寻找一种解决方案来从 spring-boot 应用程序实现 GraphQL api 调用,查询架构如下:

query {
    getDetailsByRefNumber(RefNumbers: "")
    {
        field1,
        field2,
        field3 
    } 
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何实现这个?通过以下链接之一,但没有找到任何解决方案

是否有任何基于 Java 的 Graphql 客户端可以从 java 代码调用 graphql 服务器?

java spring-boot graphql graphql-java

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

如何全局设置默认@Transactional超时

我有许多控制器,它们有很多处理事务的方法...并且我想将所有控制器的事务超时设置为 60:

@AllArgsConstructor
@Controller
public class MyController {

    @Transactional(timeout = 60)
    public void myMethod1(...) {
    }

    @Transactional(timeout = 60)
    public void myMethod2(...) {
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

如何将全局默认超时设置为 60,以便不再需要timeout = 60为每个方法指定?

transactions spring-data-jpa spring-boot

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

在 spring-boot 2.6.2 中,无法从类路径初始化 Logback 日志记录:logback-spring.groovy

我将 spring-boot 2.5.6 的 spring boot 应用程序迁移到 spring-boot 2.6.2,但从那时起,启动告诉

java.lang.IllegalStateException:无法从类路径初始化 Logback 日志记录:logback-spring.groovy 原因:ch.qos.logback.core.LogbackException:

引起原因:ch.qos.logback.core.LogbackException:文件 [file:/xyz/out/product/resources/logback-spring.groovy] 出现意外的文件扩展名。应该是 .groovy 或 .xml

如果您有解决方案,有人可以帮忙吗?谢谢

groovy spring spring-boot spring-boot-2.6.0

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

在单个类中使用多个事务的正确方法

我正在使用 Spring Boot。我可以使用@Transactional强制方法进行事务处理。有时我需要某种方法来使用两个或多个事务。

天真的方法是行不通的:

public void doActions() {
    doAction1();
    doAction2();
}

@Transactional
void doAction1() { ... }

@Transactional
void doAction2() { ... }
Run Code Online (Sandbox Code Playgroud)

因为Spring使用代理来实现事务。

通常我使用以下方法:

@Autowired
private ThisService self;

public void doActions() {
    self.doAction1();
    self.doAction2();
}

@Transactional
void doAction1() { ... }

@Transactional
void doAction2() { ... }
Run Code Online (Sandbox Code Playgroud)

它有效,但在 Spring 2.6.0 中,这种循环依赖会导致应用程序无法启动并出现可怕的错误,除非我将 spring.main.allow-circular-references 设置为 true。

我真的不明白循环引用不好的原因。但显然 Spring Boot 开发人员希望阻止这种设计,所以,我想,我最好听从他们的建议。

另一种方法是使用事务管理器并以编程方式调用事务 api:

@Autowired
private TransactionTemplate transactionTemplate;

public void doActions() {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(@NonNull TransactionStatus status) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

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

logback-classic 版本 1.2.9 中的 ContextInitializer 不支持 Groovy 配置文件

更新到 spring-boot-starter-parent 2.6.2,我的应用程序由于以下错误而无法启动

Unexpected filename extension of file [file:logback.groovy]. Should be either .groovy or .xml。查看上述类的源代码,发现以下内容:

        final String urlString = url.toString();
        if (urlString.endsWith("xml")) {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(loggerContext);
            configurator.doConfigure(url);
        } else {
            throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml");
        }
Run Code Online (Sandbox Code Playgroud)

那么看起来在最新版本中他们刚刚删除了 groovy 支持?除了回到Excel配置之外还有什么解决办法吗?谢谢

groovy spring-boot

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

找不到 SwaggerWelcomeCommon

尝试将 Springdoc 添加到 spring-boot-2.6.2 项目中。应用程序运行在嵌入式码头服务器上。使用下面的 pom.xml 设置,执行器可以正常运行。

当我尝试运行该应用程序时,出现以下错误。由于我认为这是由于其中一个依赖项而发生的,因此我尝试组织依赖项。

Description:

Parameter 3 of method indexPageTransformer in org.springdoc.webmvc.ui.SwaggerConfig required a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' that could not be found.


Action:

Consider defining a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' in your configuration.
Run Code Online (Sandbox Code Playgroud)

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>demo</groupId>
        <artifactId>demo</artifactId>
        <version>demo-1.01</version>
    </parent>

    <artifactId>demo</artifactId>
    <packaging>war</packaging>

    <properties>
        <package.plantbase>dev</package.plantbase>
        <ojdbc6.version>11.2.0.4</ojdbc6.version>
        <jetty.version>9.4.43.v20210629</jetty.version>
        <jjwt.version>0.9.1</jjwt.version>
        <reflection.version>0.9.11</reflection.version>
        <lombok.version>1.18.4</lombok.version>
        <plexus.version>2.5.2</plexus.version>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency> …
Run Code Online (Sandbox Code Playgroud)

jetty swagger spring-boot springdoc

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

在 RestController ExceptionHandler 中记录请求正文字符串

  • 最终目标:

在 RestController 的 @ExceptionHandler 中记录请求正文字符串

  • 解释

默认情况下,当 request 是无效的 json 时,springboot 会抛出一个HttpMessageNotReadableException,但该消息非常通用,不包含具体的请求正文。这使得调查变得困难。另一方面,我可以使用过滤器记录每个请求字符串,但这样日志将被太多成功的字符串淹没。我只想在请求无效时记录该请求。我真正想要的是@ExceptionHandler我将得到该字符串(以前在某个地方得到过)并记录为错误。

为了说明这个问题,我在github上创建了一个demo项目

  • 控制器:
@RestController
public class GreetController {
    protected static final Logger log = LogManager.getLogger();

    @PostMapping("/")
    public String greet(@RequestBody final WelcomeMessage msg) {
        // if controller successfully returned (valid request),
        // then don't want any request body logged
        return "Hello " + msg.from;
    }

    @ExceptionHandler({HttpMessageNotReadableException.class})
    public String addStudent(HttpMessageNotReadableException e) {
        // this is what I really want!
        log.error("{the request body string …
Run Code Online (Sandbox Code Playgroud)

java servlets spring-mvc servlet-filters spring-boot

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

Spring Boot Kafka 配置 DefaultErrorHandler?

我按照 Spring Kafka 文档创建了一个批处理消费者:

@SpringBootApplication
public class ApplicationConsumer {
  private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConsumer.class);
  private static final String TOPIC = "foo";

  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(ApplicationConsumer.class, args);
  }

  @Bean
  public RecordMessageConverter converter() {
    return new JsonMessageConverter();
  }

  @Bean
  public BatchMessagingMessageConverter batchConverter() {
    return new BatchMessagingMessageConverter(converter());
  }

  @KafkaListener(topics = TOPIC)
  public void listen(List<Name> ps) {
    LOGGER.info("received name beans: {}", Arrays.toString(ps.toArray()));
  }
}
Run Code Online (Sandbox Code Playgroud)

我能够通过定义 Spring 自动选取的以下附加配置环境变量来成功让使用者运行:

export SPRING_KAFKA_BOOTSTRAP-SERVERS=...
export SPRING_KAFKA_CONSUMER_GROUP-ID=...
Run Code Online (Sandbox Code Playgroud)

所以上面的代码有效。但现在我想自定义默认错误处理程序以使用指数退避。从参考文档中,我尝试将以下内容添加到 ApplicationConsumer 类中:

@Bean
public …
Run Code Online (Sandbox Code Playgroud)

spring apache-kafka spring-boot spring-kafka

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

使用 spring 版本 2.5.7 的 Spring Boot 无法使用 jdk 1.8 重新打包

运行 Maven 包目标时

mvn清理包

构建抛出错误:

目标 org.springframework.boot:spring-boot-maven-plugin:3.0.0-M1:repackage 失败:无法加载插件 'org.springframework.boot:spring-boot-maven-plugin 中的 mojo 'repackage': 3.0.0-M1' 由于 API 不兼容:org.codehaus.plexus.component.repository.exception.ComponentLookupException:org/springframework/boot/maven/RepackageMojo 已由更新版本的 Java 运行时(类文件版本 61.0),此版本的 Java 运行时仅识别 52.0 以下的类文件版本

我的 JAVA_HOME 指向 jdk1.8.0_73

我怎样才能让 spring-boot-maven-plugin 以重新打包的目标运行?

下面是我的模块中的 Maven 构建配置。

<build>
<resources>
    <resource>
        <directory>resources</directory>
    </resource>
</resources>
<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
            <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
            <webResources>
                <resource>
                    <directory>src/main/resources</directory>
                    <include>*.*</include>
                    <targetPath>/WEB-INF/classes</targetPath>
                </resource>
                <resource>
                    <directory>src/main/webapp/errors</directory>
                    <include>*.*</include>
                    <targetPath>/errors/</targetPath>
                </resource>
            </webResources>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.services.MyAApp</mainClass>
            <layout>war</layout>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals> …
Run Code Online (Sandbox Code Playgroud)

java maven spring-boot maven-package spring-boot-maven-plugin

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

Spring Boot中apache tomcat的默认队列大小是多少?

spring boot 中嵌入的 apache tomcat 中的默认请求队列大小是多少?

tomcat spring-boot

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