Spring Boot如何解决Whitelabel错误页面
Posted
技术标签:
【中文标题】Spring Boot如何解决Whitelabel错误页面【英文标题】:How to solve Whitelabel Error page Springboot 【发布时间】:2021-12-07 00:10:28 【问题描述】:我是 Springboot 的初学者并尝试构建一个项目,但每当我尝试将项目作为 java 应用程序运行时,我都会收到 Whitelabel 错误。
There was an unexpected error (type=Internal Server Error, status=500).
no transaction is in progress
javax.persistence.TransactionRequiredException: no transaction is in progress
这是我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.generator.marksheet</groupId>
<artifactId>Marksheet-Generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Marksheet-Generator</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin-version>3.8.1</maven-jar-plugin-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
还有我的application.properties
# database init, supports mysql too
spring.sql.init.platform = mysql
spring.sql.init.schema-locations=classpath:db/schema-mysql.sql
spring.sql.init.data-locations=classpath:db/data-mysql.sql
spring.datasource.url=jdbc:mysql://localhost:3306/MarksheetGen
spring.datasource.username=user
spring.datasource.password=admin123
spring.sql.init.mode=always
#spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
# Web
spring.thymeleaf.mode=html
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# Internationalization
#spring.messages.basename=messages/messages
# Actuator
management.endpoints.web.exposure.include=*
# Logging
logging.level.org.springframework=INFO
# logging.level.org.springframework.web=DEBUG
# logging.level.org.springframework.context.annotation=TRACE
logging.level.org.springframework.web.*=TRACE
# Maximum time static resources should be cached
spring.web.resources.cache.cachecontrol.max-age=12h
server.port = 8080
spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext
MarksheetController.java
@RestController
@RequestMapping("/student")
public class MarksheetController
@Autowired
private MarksheetService marksheetService;
@GetMapping("/details/rollnumber")
public Student showDetails(@PathVariable int rollnumber) throws Exception
Student student = marksheetService.getdetails(rollnumber);
if (student == null)
throw new Exception("Student not found - " + student);
return student;
我正在尝试在网页上返回学生详细信息,尝试了网络上所有可能的解决方案,但都没有奏效
这是完整的后备方案
no transaction is in progress
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:422)
at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3397)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1354)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1349)
at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:113)
at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95)
at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:97)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:916)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:727)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:654)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:407)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
at com.generator.marksheet.Marksheet.Generator.service.MarksheetServiceImpl$$EnhancerBySpringCGLIB$$c67ab94d.getdetails(<generated>)
at com.generator.marksheet.Marksheet.Generator.rest.MarksheetController.showDetails(MarksheetController.java:26)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:831)
这是我的 MarksheetService.java
package com.generator.marksheet.Marksheet.Generator.service;
import com.generator.marksheet.Marksheet.Generator.entity.Student;
public interface MarksheetService
public Student getdetails(int rollnumber);
和 MarksheetServiceImpl.java
package com.generator.marksheet.Marksheet.Generator.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.generator.marksheet.Marksheet.Generator.dao.MarksheetDAO;
import com.generator.marksheet.Marksheet.Generator.entity.Student;
@Service
public class MarksheetServiceImpl implements MarksheetService
@Autowired
private MarksheetDAO marksheetDAO;
@Override
@Transactional
public Student getdetails(int rollnumber)
return marksheetDAO.getdetails(rollnumber);
MarksheetDAO.java
package com.generator.marksheet.Marksheet.Generator.dao;
import com.generator.marksheet.Marksheet.Generator.entity.Student;
public interface MarksheetDAO
public Student getdetails(int rollnumber);
MarksheetDAOImpl.java
package com.generator.marksheet.Marksheet.Generator.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.generator.marksheet.Marksheet.Generator.entity.Student;
@Repository
public class MarksheetDAOImpl implements MarksheetDAO
@Autowired
private SessionFactory sessionFactory;
@Override
public Student getdetails(int rollnumber)
// get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
// now retrieve/read from database using the primary key
Student student = currentSession.get(Student.class, rollnumber);
return student;
在控制台中获取这个
Hibernate: select student0_.roll as roll1_0_0_, student0_.first_name as first_na2_0_0_, student0_.last_name as last_nam3_0_0_ from studentinfo student0_ where student0_.roll=?
【问题讨论】:
请提供您的statcktrace pastebin.com/tjmYmFDg 这里是@ScaryWombat 请将其添加到您的问题中。 @ScaryWombat 它超过了 30k 限制:( @ScaryWombat 编辑了帖子 【参考方案1】:将@Transactional 放在marksheet 服务类上。
@Transactional 公共类 MarksheetService
【讨论】:
已经没有帮助【参考方案2】:使用springboot提供的Entitymanager解决 事实证明,如果你想使用 Sessionfactory,那么你需要定义一个 config.java 并在那里提供事务管理和 Sessionfactory 的属性。
【讨论】:
很高兴你把它修好了。这个答案将来可能会对其他人有所帮助。以上是关于Spring Boot如何解决Whitelabel错误页面的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot自定义错误页面,Whitelabel Error Page处理方式
Spring Boot Actuator 不工作 - Whitelabel 错误页面
whitelabel 错误页面 404 spring boot 微服务 Rest Controller
whitelabel 错误页面 404 spring boot angular 5