我在Spotify API和API的Java Wrapper的帮助下使用Vaadin创建了一个Spring应用程序。我试图从导航到当前页面的RouteLink中获取一个Route参数,并使用该String参数来帮助我显示所需的信息。
具有RouterLink的页面具有for循环,其循环通过各种歌曲的搜索结果,然后为每首歌曲提供具有Spotify曲目ID的不同路由参数的链接。
RouterLink songPageLink = new RouterLink("Song Details", songDescription.class, new RouteParameters("trackID", track.getId()));
字符串
这是我正在处理的页面的代码:
package trackour.trackour.views.songDescription;
import org.springframework.stereotype.Component;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import trackour.trackour.views.api.APIController;
@Route("songDescription/:trackID?")
@PageTitle("Description")
@AnonymousAllowed
@Component
public class songDescription extends VerticalLayout implements BeforeEnterObserver {
private String trackID;
public songDescription() {
H1 songTitle = new H1("SONG NAME");
H2 songArtist = new H2("SONG ARTIST");
Image albumCover = new Image();
Paragraph acousticnessDetail = new Paragraph("VALUE");
Paragraph danceabilityDetail = new Paragraph("VALUE");
Paragraph energyDetail = new Paragraph("VALUE");
Paragraph instrumentalnessDetail = new Paragraph("VALUE");
Paragraph keyDetail = new Paragraph("VALUE");
Paragraph livenessDetail = new Paragraph("VALUE");
Paragraph loudnessDetail = new Paragraph("VALUE");
Paragraph modeDetail = new Paragraph("VALUE");
Paragraph tempoDetail = new Paragraph("VALUE");
Paragraph timeSignatureDetail = new Paragraph("VALUE");
Paragraph valenceDetail = new Paragraph("VALUE");
add(albumCover, songTitle, songArtist, acousticnessDetail, danceabilityDetail,
energyDetail, instrumentalnessDetail, keyDetail, livenessDetail, loudnessDetail,
modeDetail, tempoDetail, timeSignatureDetail, valenceDetail);
}
@Override
public void beforeEnter(BeforeEnterEvent event) {
trackID = event.getRouteParameters().get("trackID").orElseThrow();
}
}
型
代码按照我提供的方式工作得很好,但是一旦我尝试使用trackID
变量,就会得到不同的SpringBean异常。
下面是我想如何使用Route参数的示例:
H1 songTitle = new H1(APIController.getTrackName(trackID));
型
一旦我这样做了,我就会得到这个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'songDescription' defined in file [C:\Users\gohar\OneDrive\SFU\CMPT276\trackour\target\classes\trackour\trackour\views\songDescription\songDescription.class]: Failed to instantiate [trackour.trackour.views.songDescription.songDescription]: Constructor threw exception
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1314) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1199) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
at trackour.trackour.Application.main(Application.java:26) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.1.jar:3.1.1]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [trackour.trackour.views.songDescription.songDescription]: Constructor threw exception
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-6.0.10.jar:6.0.10]
... 22 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "String.length()" because "replacement" is null
at java.base/java.util.regex.Matcher.appendExpandedReplacement(Matcher.java:1014) ~[na:na]
at java.base/java.util.regex.Matcher.appendReplacement(Matcher.java:997) ~[na:na]
at java.base/java.util.regex.Matcher.replaceAll(Matcher.java:1181) ~[na:na]
at java.base/java.lang.String.replaceAll(String.java:2940) ~[na:na]
at se.michaelthelin.spotify.requests.AbstractRequest$Builder.setPath(AbstractRequest.java:244) ~[spotify-web-api-java-8.0.0.jar:na]
at se.michaelthelin.spotify.requests.data.tracks.GetTrackRequest$Builder.build(GetTrackRequest.java:89) ~[spotify-web-api-java-8.0.0.jar:na]
at trackour.trackour.views.api.APIController.getTrackName(APIController.java:300) ~[classes/:na]
at trackour.trackour.views.songDescription.songDescription.<init>(songDescription.java:28) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:198) ~[spring-beans-6.0.10.jar:6.0.10]
... 24 common frames omitted
型
1条答案
按热度按时间z9smfwbn1#
看起来你试图在构造函数中使用
trackID
,但这里它还没有初始化。您必须将使用trackID
的代码移动/调用到beforeEnter方法中,或者在beforeEnter
之后发生的另一个路由生命周期事件中,最好是afterNavigation
,例如当您需要访问后端并使用数据修改/填充UI时。