Tag: spring boot

SpringBoot – 无法启动嵌入式容器

当我启动springboot应用程序时,我的SpringBootLoginController类抛出此错误(无法启动嵌入式容器),如下所示。它是一个hello world类型的spring boot应用程序示例。 . ____ _ __ _ _ /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| […]

Spring Boot会在几分钟后停止解析视图

我有一个Spring Boot应用程序,突然停止解析视图,而是向我抛出404错误。 这些是我的依赖: org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.springframework spring-context org.apache.tomcat.embed tomcat-embed-jasper provided org.hibernate hibernate-validator com.jolbox bonecp ${bonecp.version} com.oracle ojdbc14 10.2.0.4.0 system ${basedir}/src/main/resources/lib/ojdbc14-10.2.0.4.0.jar com.google.guava guava ${guava.version} javax.inject javax.inject 1 javax.servlet jstl com.fasterxml.jackson.datatype jackson-datatype-hibernate4 2.5.3 org.springframework.boot spring-boot-starter-web 该应用程序启动正常,让我浏览没有任何问题。 但是,如果我停止查看并继续执行操作,当我刷新Web浏览器时,我会看到Tomcat 404页面: HTTP Status 404 – /WEB-INF/jsp/home.jsp type: Status report message: /WEB-INF/jsp/home.jsp description: The […]

使用MockServletContext进行unit testing

我已经使用Gradle设置了spring boot应用程序。 现在我明白了@EnableAutoConnfiguration根据类路径中的依赖关系来配置应用程序。 我很高兴避免所有的管道,但事情开始发生,我希望不会。 这是我的依赖项: dependencies { compile(‘org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE’) compile ‘org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE’ compile ‘org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE’ compile ‘org.springframework.boot:spring-boot-starter-data-jpa’ compile ‘com.google.guava:guava:18.0’ compile ‘com.fasterxml.jackson.datatype:jackson-datatype-jsr310’ compile ‘commons-beanutils:commons-beanutils:1.9.2’ runtime ‘org.hsqldb:hsqldb:2.3.2’ testCompile ‘org.springframework.boot:spring-boot-starter-test’ testCompile ‘com.jayway.jsonpath:json-path:2.0.0’ } 我的应用类: @ComponentScan(“org.home.project”) @SpringBootApplication //@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } UserController的一个片段: @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public […]

hibernate没有创建表但没有错误消息

我正在做一个spring-boot项目并尝试使用hibernate创建一个表,当我运行应用程序并且服务器正常启动时,我没有错误,但是表没有被创建。 StatusUpdate.java package model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.PrePersist; @Entity @Table(name=”status_update”) public class StatusUpdate { @Id @Column(name=”id”) @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(name=”text”) private String text; @Column(name=”added”) @Temporal(TemporalType.TIMESTAMP) private Date added; @PrePersist protected void onCreate() { if (added == null) { […]

Spring Boot在调试模式下阻止H2控制台

我正在尝试在调试模式下的WebIntegrationTest期间访问H2控制台。 但是,当我调试测试时,我注意到Spring Boot正在阻止H2控制台。 一旦达到断点,H2控制台也会被阻止。 我正在使用Spring Boot 1.3.1.RELEASE。 以下测试中的每个断点都会阻止H2控制台。 在断点1中,将显示登录页面。 然后我按下登录按钮但没有任何反应,直到我继续测试到下一个断点。 在断点2中,我已登录并可以执行查询。 但只有当我要进入下一个断点时,才会出现查询结果。 @Test public void whenGetById_thenCorrectId() throws InterruptedException { // do some stuff // breakpoint 1 Thread.sleep(1000); // breakpoint 2 Thread.sleep(1000); // breakpoint 3 } WebIntegrationTest配置如下: @ActiveProfiles(“local,unittest”) @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyApplication.class) @WebIntegrationTest({“spring.h2.console.enabled=true”, “server.port=8080”}) public class MyResourceTest { 如何将H2-in-memory数据库与调试模式分离?

Spring AuthenticationFailureHandler和WebSecurityConfigurerAdapter loginPage()

编辑:解决了。 在这篇文章后看到我的评论 我目前正在使用Spring-Security实现Web应用程序。 我已经实现了一个自定义AuthenticationFailureHandler ,它检查用户是否经常尝试使用错误的凭据登录(并阻止他几分钟)。 但正常的登录失败应该将用户重定向到登录页面,参数错误( /login?error )。 此页面显示错误消息,例如“您输入的密码错误” AutenticationFailureHandler看起来像这样(没有无代码的代码) public class CustomAuthenticationHandler implements AuthenticationFailureHandler { // Some variables @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { // some logic here.. request.setAttribute(“param”, “error”); response.sendRedirect(“/login?error”); } 我的WebApplicationSecurity类如下所示: @Configuration @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired CustomAuthenticationHandler customAuthenticationHandler; @Override protected void […]

具有多个数据源Oracle和H2的Spring Boot

我为我的角项目开发了一个Spring Boot RestController并遇到了问题。 在我的rest服务逻辑中,我使用了两个不同的数据库来获取数据。 在这里您可以看到数据源配置: [application.properties] #datasource1 spring.datasource.url=[url] spring.datasource.username=[username] spring.datasource.password=[password] spring.datasource.driverClassName=org.h2.Driver #datasource2 spring.secondDatasource.url=[url] spring.secondDatasource.username=[username] spring.secondDatasource.password=[password] spring.secondDatasource.driverClassName=oracle.jdbc.OracleDriver [DatasourceConfig.java] @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource h2DataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix=”spring.secondDatasource”) public DataSource oracleDataSource() { return DataSourceBuilder.create().build(); } 记录输出:(此问题不会引发exception) … 2016-11-22 13:20:25.853 [INFO ] 1 [main] dbsApplication : Started Application in 7.757 seconds (JVM running for 12.515) […]

如何在执行测试时阻止我的Spring Boot Batch应用程序运行?

我有一个Spring Boot Batch应用程序,我正在编写集成测试。 当我执行测试时,整个批处理应用程序运行。 如何只执行测试中的应用程序代码? 这是我的测试代码。 执行时,将运行整个批处理作业步骤(阅读器,处理器和编写器)。 然后,测试运行。 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = BatchApplication.class)) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class }) public class StepScopeTestExecutionListenerIntegrationTests { @Autowired private FlatFileItemReader reader; @Rule public TemporaryFolder testFolder = new TemporaryFolder(); public StepExecution getStepExection() { StepExecution execution = MetaDataInstanceFactory.createStepExecution(); return execution; } @Test public void testGoodData() throws Exception { //some test code on one met […]

响应返回给客户端后清理

语境: 我有一个用于下载zip存档的端点。 @GetMapping public DeferredResult download(/**params**/) { } 由于文件和文件大小的数量,将所有文件保存在内存中是不可能的(即我必须从外部服务中读取文件块,将它们存储到临时目录,创建zip存档,以块的forms写入文件zip,在归档完成后删除所有临时文件 – 此时我只留下磁盘上的存档 – 然后将zip流回客户端)。 我已经设法实现了这个function,但我不确定哪个是请求后清理的最佳方法(请注意,该服务由多个客户端使用 – 因此多个下载应该能够同时进行)。 目前,我正在使用HandlerInterceptor ,它在preHandle方法中为归档生成一个随机名称,并将其作为请求属性传递。 请求属性将传递给生成存档的服务,并用作存档名称。 然后,在拦截器的afterCompletion方法中,我从请求属性中读取存档名称并删除存档。 class ZipInterceptor implements HandlerInterceptor { public static final String ZIP_ATTRIBUTE_NAME = “zipName”; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if(isApplicable(request)){ request.setAttribute(ZIP_ATTRIBUTE_NAME, generateZipName()); } return true; } @Override public void postHandle(HttpServletRequest […]

在spring boot 2.0.0中设置jvmRoute

对于粘性会话,我需要设置嵌入式tomcat的jvmRoute。 实际上只有一个 System.setProperty(“jvmRoute”, “node1”); 是必需的,但我想设置一个via application.properties可配置属性。 我不知道如何以及何时使用@Value注释属性进行设置。 使用@PostConstruct,如此处所述,它不起作用(至少不在spring boot 2.0.0.RELEASE中) 我到目前为止找到的唯一方法是 @Component public class TomcatInitializer implements ApplicationListener { @Value(“${tomcat.jvmroute}”) private String jvmRoute; @Override public void onApplicationEvent(final ServletWebServerInitializedEvent event) { final WebServer ws = event.getApplicationContext().getWebServer(); if (ws instanceof TomcatWebServer) { final TomcatWebServer tws = (TomcatWebServer) ws; final Context context = (Context) tws.getTomcat().getHost().findChildren()[0]; context.getManager().getSessionIdGenerator().setJvmRoute(jvmRoute); } } } […]