Tag: spring boot

如何在Spring Boot中使用Hibernate / JPA返回多级json

我有一个Postgres数据库,有4个表父母,子女,团体和Group_Membership。 群组可以有多个父母,父母可以有多个群组。 父母可以有多个孩子,但孩子只能有一个孩子。 这是架构的愚蠢版本。 我正在使用Spring Boot和Hibernate JPA。 Parent.java @Entity @Table(name = “parents”) public class Parent { @Id @GeneratedValue @Column(name=”parent_id”) private Long parentId; @Column(name= “first_name”) private String firstName; @Column(name= “last_name”) private String lastName; @OneToMany(mappedBy=”parent”) private Set children; @ManyToMany(cascade = { CascadeType.ALL }) @JoinTable( name= “Group_Membership”, joinColumns = { @JoinColumn(name = “parent_id”) }, inverseJoinColumns = { @JoinColumn(name […]

Spring启动时使用构造函数参数初始化bean

我需要在启动时使用带有构造函数参数的Spring Boot初始化以下PointQuadTree类,并使该对象在整个应用程序中可用。 构造函数参数’minX,maxX,…’需要来自application.properties文件。 PointQuadTree public class PointQuadTree { private final Bounds mBounds; public PointQuadTree(double minX, double maxX, double minY, double maxY) { this(new Bounds(minX, maxX, minY, maxY)); } … } 边界 public class Bounds { public final double minX; public final double minY; public final double maxX; public final double maxY; public final double midX; […]

如何在bean实例化之前记录spring引导应用程序的所有活动属性?

有一个问题要求记录活动配置,有一个正确的答案,但问题是只有在所有bean都被正确实例化的情况下才会记录配置。 如果应用程序在启动时崩溃,我甚至(主要)记录所有属性。 我的问题更具体: 如何在bean实例化之前记录spring引导应用程序的所有活动属性?

如何在Spring Data Rest应用程序中创建实体之间的引用

我正在尝试使用Spring Boot + Data Rest + JPA构建简单的应用程序。 A具有一对多关系的Category和Book实体: @Entity public class Category { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @OneToMany(cascade = CascadeType.ALL, mappedBy = “category”) private Set books; …getters & setters next… } 和 @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne private […]

使用Java DSL进行Spring集成的动态TCP服务器

我试图通过读取包含连接细节的属性文件来创建TCP服务器和客户端。 我在下面的参考文档(9.20动态和运行时集成流程)的帮助下使用动态和运行时集成流程 代码在创建客户端时工作正常,但是当我使用相同代码中的更改创建服务器时,如下所示: IntegrationFlow flow = f -> f .handle(Tcp.inboundAdapter(Tcp.netServer(2221) .serializer(TcpCodecs.crlf()) .deserializer(TcpCodecs.lengthHeader1()) .id(“server”))) .transform(Transformers.objectToString()); IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register(); 我收到以下错误: Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.String] for method match: [public java.lang.Class org.springframework.integration.dsl.IntegrationComponentSpec.getObjectType(), public S org.springframework.integration.dsl.MessageProducerSpec.outputChannel(java.lang.String), public S org.springframework.integration.dsl.MessageProducerSpec.outputChannel(org.springframework.messaging.MessageChannel), public org.springframework.integration.ip.dsl.TcpInboundChannelAdapterSpec org.springframework.integration.ip.dsl.TcpInboundChannelAdapterSpec.taskScheduler(org.springframework.scheduling.TaskScheduler), public S org.springframework.integration.dsl.MessageProducerSpec.errorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy), public S org.springframework.integration.dsl.MessageProducerSpec.phase(int), public S org.springframework.integration.dsl.MessageProducerSpec.autoStartup(boolean), public S org.springframework.integration.dsl.MessageProducerSpec.sendTimeout(long)] […]

使用属性值的RequestMapping进行Spring Boot REST控制器测试

关于Spring Boot REST Controller的unit testing,我遇到了@RequestMapping和应用程序属性的问题。 @RestController @RequestMapping( “${base.url}” ) public class RESTController { @RequestMapping( value = “/path/to/{param}”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE ) public String getStuff( @PathVariable String param ) { // implementation of stuff } } 我正在处理应用程序的几个配置文件,因此我有几个application-{profile}.properties文件。 在每个文件中, base.url属性值已设置并存在。 我还有一个不同的Spring Context配置用于测试,只有一个Bean与高效版本不同。 使用JUNit和Mockito / RestAssured,我的unit testing如下所示: @ActiveProfiles( “dev” ) @RunWith( SpringJUnit4ClassRunner.class ) @SpringApplicationConfiguration( classes […]

spring – ApplicationContext registerBean自动assembly失败,但getBean在Spring 5中工作

我正在使用一个使用动态bean注册的配置类: @Configuration public class ConfigClass { @Autowired private GenericApplicationContext applicationContext; @PostConstruct private void init() { System.out.println(“init”); applicationContext.registerBean(“exService”, ExecutorService.class, () -> Executors.newFixedThreadPool(10), bd -> bd.setAutowireCandidate(true)); System.out.println(“init done”); } } 如果我尝试自动assemblybean,则应用程序启动失败, Field exService in com.example.DemoApplication required a bean of type ‘java.util.concurrent.ExecutorService’ that could not be found.出现错误Field exService in com.example.DemoApplication required a bean of type ‘java.util.concurrent.ExecutorService’ that could […]

如何使用Spring Boot将H2连接到远程数据库而不是嵌入模式?

对于我的小型Spring Boot应用程序,我在src / main / resources下有这个配置: server.port = 8090 spring.datasource.driverClassName = org.h2.Driver spring.datasource.url = jdbc:h2:file:~/stapler 我知道此配置已正确选取,因为应用程序启动日志中存在有效的端口号8090。 还有一个@PostConstruct initDb()方法,它创建数据并将数据插入该数据库的2个表中: package com.avk.stapler.init; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jdbc.core.JdbcTemplate; import javax.annotation.PostConstruct; @SpringBootApplication public class DbInitializer { @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(DbInitializer.class, args); } @PostConstruct private void initDb() { System.out.println(“Creating table […]

Spring Boot集成测试不会读取属性文件

我想创建集成测试,其中Spring Boot将使用@Value注释从.properties文件中读取值。 但每次我运行测试时,我的断言都会失败,因为Spring无法读取值: org.junit.ComparisonFailure: Expected :works! Actual :${test} 我的测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebTests.ConfigurationClass.class, WebTests.ClassToTest.class}) public class WebTests { @Configuration @ActiveProfiles(“test”) static class ConfigurationClass {} @Component static class ClassToTest{ @Value(“${test}”) private String test; } @Autowired private ClassToTest config; @Test public void testTransferService() { Assert.assertEquals(config.test, “works!”); } } src / main / resource包下的application-test.properties包含: test=works! 这种行为的原因是什么?我该如何解决? 任何帮助高度赞赏。

@EnableFeignClients和@FeignClient在自动assembly’FeignContext’NoSuchBeanException时失败

我正在编写的微服务需要与我们平台中的其他微服务进行通信。 在这次尝试中,我们的理想解决方案是Spring Cloud Netflix Feign ,实现@FeignClient 。 但是,当我尝试@Autowired ReviewProvider时,我正面临下面的例外: 例外(原因) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘org.springframework.cloud.netflix.feign.FeignContext’ available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093) at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:155) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ReviewProvider.java @FeignClient(“http://metadata-reviews”) public interface ReviewProvider { @RequestMapping(path = “sessions”, method = POST) ReviewSessionDTO createSession(); } ReviewProvider.java @RunWith(SpringRunner.class) @ActiveProfiles(INTEGRATION) @ContextConfiguration(classes = AppEntry.class) @AutoConfigureTestDatabase(replace = […]