Tag: spring boot

如何在Spring(Boot)中修饰REST响应?

我有一个Spring Boot应用程序,它返回被编码为JSON响应的各种对象,我想对它们进行后处理并向某些超类添加信息。 有没有办法过滤,拦截等来自我的REST端点的对象响应,然后才能用Jackson编码为JSON。 filter无法工作,因为它在HttpServlet{Request,Response}级别运行。

Spring Boot:使用database和application.properties进行配置

我需要在数据库中保存Spring Boot应用程序的配置。 是否可以将数据库信息存储在application.properties并使用它们连接到数据库并从那里检索所有其他属性? 所以我的application.properties看起来像: spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydb spring.datasource.username=user spring.datasource.password=123456 spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect 其他配置将从数据库中获取,如下所示: @Configuration @PropertySource(value = {“classpath:application.properties”}) public class ConfigurationPropertySource { private final ConfigurationRepository configurationRepository; @Autowired public ConfigurationPropertySource(ConfigurationRepository configurationRepository) { this.configurationRepository = configurationRepository; } public String getValue(String key) { ApplicationConfiguration configuration = configurationRepository.findOne(key); return configuration.getValue(); } } 使用ApplicationConfiguration作为Entity 。 但Spring Boot无法从数据库中获取配置。

JobBuilderFactory.get(job).incrementer(RunIdIncrementer)的function是什么?

我正在使用Spring-Boot开发一个Spring-Batch项目,一切都顺利进行。 我已经完成了一些春季批量示例(包括一些来自spring.io),但我不确定其中的一些内容是什么,“它只是有效”并不能满足我的需求。 我的spring boot主类实现了CommandLineRunner ,对于这个特定的工作,初始设置看起来像 @Bean public Job myJob(JobExecutionListenerSupport listener) { return myJobBuilderFactory.get(JOB) .listener(listener) .start(myStep()) .build(); } 哪个引起了 java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:809) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.bjc.providermodel.maintenance.MaintenanceApplication.main(MaintenanceApplication.java:20) [classes/:?] Caused by: org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already […]

使用application.properties将Spring Boot中logback.xml的位置外部化

如何使用application.properties在Spring Boot中自定义logback.xml的位置? 我试过下面但是没有用。 application.properties logging.config = /home/dev-01/Documents/logback.xml 如果logback.xml位于资源文件夹中,则它正在工作,但如果它位于项目外部,则无法正常工作。 如果我提供环境变量,它也可以工作 -Dlogging.config=/home/dev-01/Documents/logback.xml

Spring Boot,Websockets无法从Session获取用户(即java.security.Principal)

使用Spring Boot 1.2.1.RELEASE和Spring Websockets 。 有一个部署运行时问题,当运行嵌入式Jetty 9 ,除了localhost之外的其他地方应用程序部署时,我无法成功伪造用户( java.security.Principal )。 我咨过过 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-authentication http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-server-runtime-configuration 下面的配置(我相信)已经“升级”了一个请求 @Configuration @EnableWebSocketMessageBroker @EnableScheduling public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry registry) { // see http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay // requires an external broker like AMQP or RabbitMQ //registry.enableStompBrokerRelay(“/queue/”, “/topic/”); // XXX This might wind up being the impl we actually deploy; […]

Spring Boot如何忽略HttpStatus Exceptions

我正在使用Spring Boot构建应用程序。 这个应用程序是分布式的,这意味着我有多个API相互调用。 我的一个底层服务与数据库交互并使用请求的数据进行响应。 如果对未存在的ID进行了请求,我将使用404 HttpStatus进行响应: return new ResponseEntity(HttpStatus.NOT_FOUND); (与某些操作中的400错误相同,或者删除条目时的错误等)。 问题是我有一些其他的Spring Boot应用程序调用这些API,抛出一个org.springframework.web.client.HttpClientErrorException: 404 Not Found Exception,当他们请求时,在这个例子中是一个不存在的条目。 但404状态代码是有意的,不应该返回此exception(导致我的Hystrix断路器调用其回退function)。 我怎么解决这个问题? 在我的代码中对服务的调用是这样实现的: ResponseEntity data = restTemplate.getForEntity(url, Object.class); 我的RestTemplate设置如下: private RestTemplate restTemplate = new RestTemplate();

从单个主机注册Spring Boot Eureka Client的多个实例

UPDATE 此repo中的README已更新,以在接受的答案中演示解决方案。 我正在使用基于本指南的Spring Boot Eureka服务注册和发现的简单示例。 如果我启动一个客户端实例,它会正确注册,并且它可以通过DiscoveryClient看到自己。 如果我使用不同的名称启动第二个实例,它也可以。 但是,如果我启动两个具有相同名称的实例,则仪表板仅显示1个正在运行的实例,而DiscoveryClient仅显示第二个实例。 当我杀死第二个实例时,第一个实例再次通过仪表板和发现客户端可见。 以下是我正在采取的步骤以及我所看到的步骤的更多细节: 尤里卡服务器 启动服务器 cd eureka-server mvn spring-boot:run 访问位于http:// localhost:8761的Eureka仪表板 请注意,尚未注册“实例” 尤里卡客户 启动客户端 cd eureka-client mvn spring-boot:run 直接访问客户端: http:// localhost:8080 / /whoami端点将显示客户端对其应用程序名称和端口的自知之明 { “springApplicationName”:”eureka-client”, “serverPort”:”8080″ } /instances端点最多需要一分钟才能更新,但最终应显示已在Eureka Discovery Client中注册的所有eureka-client实例。 [ { “host”:”hostname”, “port”:8080, “serviceId”:”EUREKA-CLIENT”, “uri”:”http://hostname:8080″, “secure”:false } ] 您现在也可以再次访问Eureka dashoboard并在那里看到它。 使用其他名称启动另一个客户端 您可以通过执行以下操作看到另一个客户端将被注册: cd eureka-client mvn spring-boot:run -Dspring.application.name=foo […]

jar文件外的application.properties如何

如spring-boot-reference中所述 : 在应用程序类路径上(例如在jar中),您可以拥有一个application.properties,它为name提供合理的默认属性值。 在新环境中运行时,可以在jar外部提供覆盖名称的application.properties 我将重复的application.properties与覆盖的名称放在与jar文件相同的路径上,但是在运行应用程序时: java -jar target/myproject-0.0.1-SNAPSHOT.jar 名称值未被覆盖,它仍然指的是jar文件中的application.properties内部的一个。 我也尝试过: java -Dspring.config.location=/target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar 但它不起作用,请帮忙。 编辑 当我将当前目录更改为target并运行它时,它可以正常工作。 java -jar myproject-0.0.1-SNAPSHOT.jar 为什么? 为什么不能在路径之外运行呢?

在Spring Boot中获取EntityManager的句柄

有没有办法获得给定实体对象的EntityManager句柄? 我正在使用带有JPA启动器的spring boot 1.2.3,我还使用@configuration显式配置了多个数据源 我已经检查了[已解决] SPRING BOOT对entityManager的访问权限 ,它似乎没有回答这个问题。 谢谢。 编辑:我添加了如何定义数据源的说明: @Component @Configuration public class DataSources { @Bean @Primary @ConfigurationProperties(prefix=”first.datasource”) public DataSource getPrimaryDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix=”second.datasource”) public DataSource getSecondDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix=”third.final.datasource”) public DataSource getThirdFinalDataSource() { return DataSourceBuilder.create().build(); } } 在我的application.yml中,我有以下部分 first.datasource: name: ‘first_datasource’, #other attributes… second.datasource: name: ‘second_datasource’, #other […]

如何触发预定的Spring批处理作业?

我希望能够使用REST控制器开始我的工作,然后当作业启动时,它应该按计划运行,直到我再次使用REST停止它。 所以这是我的控制器: @RestController public class LauncherController { @Autowired JobLauncher jobLauncher; @Autowired Job job; @RequestMapping(“/launch”) public String launch() throws Exception { … jobLauncher.run(job, jobParameters); } 这是Batch conf的一部分: @Configuration @EnableBatchProcessing @EnableScheduling public class BatchConfiguration { @Autowired public JobBuilderFactory jobBuilderFactory; @Autowired public StepBuilderFactory stepBuilderFactory; @Scheduled(cron = “0/5 * * * * ?”) @Bean public Job job() { return […]