标签: spring-boot

在 Spring 5 JPA findOne() 中获取 `Long 无法转换为 Example<S>`

我在argument mismatch; Long cannot be converted to Example<S>下面的代码中收到了 findOne 调用:

public Optional<AuditEvent> find(Long id) {
    return Optional.ofNullable(persistenceAuditEventRepository.findOne(id))
        .map(auditEventConverter::convertToAuditEvent);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码正在转换为 Spring 5 和 Spring Boot 2。它在原始 Spring 4 和 Spring Boot 1 应用程序中运行良好。

任何想法我需要将上述代码转换为什么?

spring spring-boot

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

Spring boot 无法创建多个兔子连接工厂

我正在尝试通过 xml 使用 Spring Boot 应用程序连接到两个不同的rabbitmq 集群并使用它们。当在应用程序上下文中创建单个 rabbit:connection-factory bean 时,它运行良好。但是,当添加第二个时,它无法启动应用程序,并显示错误“org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration 中的方法 rabbitListenerContainerFactory 的参数 1 需要一个 bean,但找到了 2 个:”。如何为每个集群创建不同的工厂?如果这不是正确的方法,请提出一种替代方法?

这是xml片段:

<rabbit:connection-factory id="firstConnectionFactory" connection-factory="firstSpringConnectionFactory"  />
<rabbit:connection-factory id="secondConnectionFactory" connection-factory="secondSpringConnectionFactory"/>
<bean id="firstSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
    <property name="useSSL" value="${rabbitmq.ssl.enabled}" />
    <property name="host" value="${rabbitmq.first.host}"/>
    <property name="virtualHost" value="${rabbitmq.vhost}"/>
    <property name="port" value="${rabbitmq.cluster.port}"/>
    <property name="username" value="${rabbitmq.user}"/>
    <property name="password" value="${rabbitmq.first.password}"/>
</bean>

<bean id="secondSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
    <property name="useSSL" value="${rabbitmq.ssl.enabled}" />
    <property name="host" value="${rabbitmq.second.host}"/>
    <property name="virtualHost" value="${rabbitmq.vhost}"/>
    <property name="port" value="${rabbitmq.cluster.port}"/>
    <property name="username" value="${rabbitmq.user}"/>
    <property name="password" value="${rabbitmq.second.password}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

和侦听器容器代码:

ConnectionFactory cf = rabbitConnectionFactory;//One of …
Run Code Online (Sandbox Code Playgroud)

spring spring-amqp spring-boot

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

无法使用 spring boot jpa 构建 Hibernate SessionFactory

起初我有一个名为 person 的模型。它运行良好并在我的数据库中创建了一个表。在我添加了一个带有外键的新模型后,我遇到了这个问题。我没有dbconfig.java。我只有一个application.properties。像这样。

spring.thymeleaf.mode=LEGACYHTML5


# Database
spring.datasource.url=jdbc:mysql://localhost:3306/db_example?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# Hibernate
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

spring.jackson.serialization.indent-output=true
Run Code Online (Sandbox Code Playgroud)

这是模型

人.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity 
@NoArgsConstructor
@AllArgsConstructor
@Data
@NamedQuery(name="Person.withNameAndcollegeNamedQuery", query = "select p from Person p where p.name=?1 and p.college=?2")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private String college;
    private String title;
}
Run Code Online (Sandbox Code Playgroud)

项目,java 包com.example.model; …

java spring hibernate spring-boot

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

如何在spring boot中从属性文件为@Order注释设置值

我需要将以下 bean 自动装配到 a List,并且需要List订购我的。这就是我的做法:

@Service
@Order(1)
public class Slave1 implements Slave {}

@Service
@Order(2) //instead of hardcoding I need the value to be picked up externally
public class Slave2 implements Slave {}

@Autowire
List<Slave> slaves;
Run Code Online (Sandbox Code Playgroud)

但我希望从application.properties文件中获取订单值。这可能吗?我可以为@Order属性文件中的注释设置值吗?

java spring spring-boot

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

Intelijj IDEA springboot devtools

我使用 Springboot 创建我的应用程序,所以在 Springboot Doc 中说如果我使用 devtools,tomcat 将重新启动我的代码中的所有更改,但是我使用 maven 面板来运行我的应用程序只需双击 spring-boot:run 插件,但是当我更改了我的 tomcat 不会重新启动的任何代码。

我怎样才能解决这个问题?tks

intellij-idea spring-boot spring-boot-devtools

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

使用 spring-boot 应用程序自动压缩/优化图像

有没有办法在 spring-boot-application 中自动压缩/优化图像?在我的应用程序中,用户可以自己将任何图像放入文件夹中,我无法确定它们是否以最佳方式压缩。由于它们不是通过应用程序上传的,我也无法创建优化版本。

所以我想做的是在请求图像后压缩/优化图像,并可能将它们保存在一种“图像缓存”中一段时间​​。

或者是否有一个tomcat/apache-module,它已经可以开箱即用地做这种事情?

谢谢你的帮助

java spring tomcat image-compression spring-boot

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

微服务配置和eureka服务先启动哪个?

我正在使用 spring boot 和 netflix OSS 在微服务中创建一个简单的项目来弄脏我的手。我创建了两个服务

  1. 必须在发现(尤里卡)服务中注册自己的配置服务。
  2. 发现服务需要运行配置服务来获取其配置。

现在,当我启动这些服务时,由于相互依赖,这两个服务都失败了。解决此问题的最佳实践是什么以及首先开始哪个实践。

PS:-我知道我正在创建循环依赖,但是处理这种情况的方法是什么,我想将 eureka 配置也保留在配置服务器上

谢谢

intellij-idea netflix spring-boot microservices netflix-eureka

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

Springboot:如何执行具有真实依赖项的集成测试?

我现在开始学习 Java 和 Spring boot,我在集成测试中遇到了依赖注入的一些问题。我在src/main/java/com/rfd/domain/services下有一个名为 TransactionService 的类,它被标记为 @Service 并且它有另一个依赖项,其中一个是由 Spring Boot 创建的存储库。当我启动应用程序时,它会正确启动,因此我假设依赖项正在正确解析。这是总结类:

package com.rfd.domain.services;

import allNeededImports

@Service
public class TransactionsService {

    @Autowired
    private KambiTransactionRepository kambiTransactionRepository;

    @Autowired
    private TransactionFactory transactionFactory;

    public List<Transaction> retrieveTransactions(String couponExternalId) throws InvalidTransactionException {
        // someCode
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我在/src/test/java/com/rfd/integrationtests/domain/services下有一个 TransactionsServiceTests 类:

package com.rfd.integrationtests.domain.services;
import allNeededImports

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@DataMongoTest
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
public class TransactionsServiceTests {

    @Autowired
    private TransactionsService transactionsService;

    @Test
    public void retrieveTransactions_happyPathMultipleTransactions_transactionsRetrieved() throws InvalidTransactionException {
        // test code
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试启动测试时,收到以下错误:

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“com.rfd.domain.services.TransactionsService”类型的合格bean:预计至少有1个bean有资格作为自动装配候选。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} …

java integration-testing dependency-injection spring-boot

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

Spring Boot Camel 测试

我需要在 Spring Boot 应用程序中测试 Camel 路由。我有 Spring boot 主类,其中声明了所有必需的 bean。我正在使用 CamelSpringJUnit4ClassRunner.class。在 @ContextConfiguration 中添加了我的 Spring boot 主类,因为它包含所有配置。我没有单独的配置类。

我在我的测试类中自动装配了 CamelContext:

@Autowired
CamelContext camelContext;
Run Code Online (Sandbox Code Playgroud)

但是测试失败并显示错误:

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:

没有可用的“org.apache.camel.CamelContext”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。

依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

apache-camel spring-boot junit5 camel-test

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

使用 RabbitTemplate 或 AmqpTemplate 中的哪一个?

我编写了以下程序,Spring Boot该程序运行良好。但是,问题是我不确定我是否应该使用RabbitTemplateAmqpTemplate. 一些在线示例/教程使用,RabbitTemplate而另一些使用AmqpTemplate.

请指导什么是最佳实践以及应该使用哪一个。

@SpringBootApplication
public class BasicApplication {

    private static RabbitTemplate rabbitTemplate;
    private static final String QUEUE_NAME = "helloworld.q";

    //this definition of Queue is required in RabbitMQ. Not required in ActiveMQ
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    public static void main(String[] args) {
        try (ConfigurableApplicationContext ctx = SpringApplication.run(BasicApplication.class, args)) {
            rabbitTemplate = ctx.getBean(RabbitTemplate.class);
            rabbitTemplate.convertAndSend(QUEUE_NAME, "Hello World !");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

spring amqp rabbitmq spring-amqp spring-boot

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