Tag: spring boot

在打包到jar后,Spring启动应用程序不提供静态资源

我有一个应用程序,通过ide或命令行启动时工作得很好:mvn spring-boot:run。 但是当我将它打包到jar中时,我无法访问静态资源(未找到404)。 我不想将静态文件存储在资源漏洞中,所以每次我需要更改静态文件时都不必重新加载服务器。 所以我在我的pom.xml中使用了这个插件: maven-resources-plugin 2.6 copy-resources validate copy-resources ${basedir}/target/classes/static src/main/webapp true 我可以看到文件正在两个目录“static”中复制。 这是我的资源处理程序配置: @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/**”).addResourceLocations(“/”); } 控制器RequestMappings工作正常,问题只与静态资源有关。

MappingMongoConverter setMapKeyDotReplacement不起作用

我的代码如下链接: 如何在Spring-Boot中自定义MappingMongoConverter(setMapKeyDotReplacement)而不破坏自动配置? @Override @Bean public MappingMongoConverter mappingMongoConverter() throws Exception { DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.mongoDbFactory()); MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext()); converter.setCustomConversions(this.customConversions()); converter.setMapKeyDotReplacement(“_”); return converter; } 但是,如果我尝试解析这个JSON,即Java JSONObject: { “Dr.Web category”: “known infection source”, “categories”: [ “parked”, “uncategorized” ] } 这种例外总会发生。 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mapping.model.MappingException: Map key Dr.Web category contains dots […]

使用Spring Boot 1.4从Spring Boot Test中排除Spring集成

我最近开始使用Spring Boot 1.4和新的Spring Boot Testfunction。 我正在使用spring-boot-starter-integration与Spring Boot 1.4.2.RELEASE并尝试使用新的@DataJpaTest测试我的存储库 当我运行我的测试时,我得到一个关于没有限定bean的exception。 有问题的bean是我的集成bean的handler 。 如何在JPA测试期间排除集成运行? 申请类别: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(“integration.xml”) public class AutomateResultConsumerApplication { public static void main(String[] args) { SpringApplication.run(AutomateResultConsumerApplication.class, args); } } 测试类: @RunWith(SpringRunner.class) @DataJpaTest public class SampleHistoryRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private SampleHistoryRepository sampleHistoryRepository; @Test public void findAllByObjectIdAndField() throws […]

在Spring Boot服务上运行有限数量的线程

我目前正在使用spring boot开发一个Web应用程序,我在服务层中遇到了问题。 我的服务层上有一个沉重的方法。 如果多个用户调用相同的服务,则应用程序会因内存不足而停止。 所以我想限制该方法的并行运行线程的数量。 到目前为止,我已经出现了在该方法上使用synchronized 。 但它会将它限制为单线程方法。 @Service public class DocumentService{ private synchronized void doReplacement(){ //should have limited no of multi threads (eg. 3) } private void normalMethod(){ //no restrictions } } 我该怎么做才能完成这项任务。 任何帮助,将不胜感激。

需要一个无法找到Spring类型的bean

Spring Boot应用程序无法启动? 这是我的项目结构 以下是我的主要课程。 package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Start { public static void main(String[] args) { SpringApplication.run(Start.class, args); } } 我的控制台是 2017-11-28 11:48:52.187 WARN 7316 — [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through […]

将MySQL连接到Spring应用程序

我想使用MySQL数据库,而不是像hsqldb那样使用运行时数据库。 我克隆了这个存储库 ,它使用hsqldb作为其数据库。 因为我想学习如何使用基于rest的spring应用程序的关系数据库。 所以我对pom.xml进行了以下更改:更改了pom.xml: 4.0.0 org.springsource.restbucks restbucks war 1.0.0.BUILD-SNAPSHOT Spring RESTBucks org.springframework.boot spring-boot-starter-parent 1.1.5.RELEASE Evans-RC1 8.0.9 org.springframework.boot spring-boot-starter-data-rest org.springframework.boot spring-boot-starter-data-jpa org.hibernate hibernate-entitymanager org.springframework.boot spring-boot-starter-test org.jadira.usertype usertype.extended 3.2.0.GA com.fasterxml.jackson.datatype jackson-datatype-jsr310 org.hibernate hibernate-entitymanager mysql mysql-connector-java org.springframework spring-tx-events 1.0.0.BUILD-SNAPSHOT org.projectlombok lombok 1.14.4 provided com.jayway.jsonpath json-path org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 org.springframework.boot spring-boot-maven-plugin spring-libs-snapshot http://repo.spring.io/libs-snapshot true spring-libs-snapshot http://repo.spring.io/libs-snapshot true […]

Spring Boot基于角色的认证

我有一个关于基于Spring Boot角色的身份validation的问题。 基本上,我想有用户和管理员,我想阻止用户访问管理资源。 所以我创建了一个SecurityConfig类: package test; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; @Configuration @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser(“user1”).password(“password1”).roles(“USER, ADMIN”) .and() .withUser(“user2”).password(“password2”).roles(“USER”); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers(“/service/test”).access(“hasRole(‘USER’) or hasRole(‘ADMIN’)”) .antMatchers(“/service/admin”).access(“hasRole(‘ADMIN’)”); } } 这是我的小REST服务: package […]

为什么Maven + Spring Boot会创建巨大的jar文件?

我有以下Maven项目结构: parent_project +–main_application +–domain_models_and_repository +–module_1 +–module_2 +–module_3 以下简化的POMS: parent_project.pom [Spring Boot dependencies] main_application domain_models_and_repository module_1 module_2 module_3 org.springframework.boot spring-boot-maven-plugin main_application parent_project domain_models_and_repository module_1 module_2 module_3 module_1 parent_project domain_models_and_repository module_2 parent_project domain_models_and_repository module_3 parent_project domain_models_and_repository module_1 module_2 实际上我有更多的模块,其中更多的是其他人的依赖。 当我运行mvn install我得到一个主应用程序的1.2GB文件。 我注意到所有模块的所有依赖关系都被组装到模块中。 因此,许多jar文件被多次组装到文件中。 我怎么能避免这个?

Spring Boot YARN无法在Hadoop上运行2.8.0客户端无法访问DataNode

我正在尝试运行Spring Boot YARN示例(Windows上的https://spring.io/guides/gs/yarn-basic/ )。 在application.yml我将fsUri和resourceManagerHost更改为指向我的VM的主机192.168… 但是,当我试图运行应用程序Exceprion出现时: DFSClient: Exception in createBlockOutputStream java.net.ConnectException: Connection timed out: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:531) at org.apache.hadoop.hdfs.DFSOutputStream.createSocketForPipeline(DFSOutputStream.java:1508) at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.createBlockOutputStream(DFSOutputStream.java:1284) at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.nextBlockOutputStream(DFSOutputStream.java:1237) at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:449) [2017-05-27 19:59:49.570] boot – 7728 INFO [Thread-5] — DFSClient: Abandoning BP-646365587-10.0.2.15-1495898351938:blk_1073741830_1006 [2017-05-27 19:59:49.602] boot – 7728 INFO [Thread-5] — DFSClient: Excluding […]

Spring Boot:如何使用多个模式并动态选择在运行时为每个请求使用哪个模式

前提:我选择这样做是因为我最终可能会有几千个模式,每个模式都有(其中包括)1个表和几百万个条目。 另一种方法是在一个模式中拥有一个表,其中包含几十亿个条目。 详细阐述这个问题的最好方法是提供一个简单的例子。 考虑以下: User.java @Entity(name = “user”) public class User { @Id @GeneratedValue @Column(name = “id”) private Long id; @Column(name = “username”) private String username; // getters and setters… } UserDao.java @Repository public interface UserDao extends CrudRepository {} UserService.java public interface UserService { User getUser(Long id); } UserServiceBean.java @Transactional @Service public class UserServiceBean implements […]