【发布时间】:2021-01-07 14:19:44
【问题描述】:
我一直在实现一个简单的 quarkus 应用程序,用于创建与 Alfresco 一起使用的 CMIS API。关注this tutorial(只是提供一点见解)
一切都很顺利,直到我决定使用属性来传递会话参数。我已将这些添加到 main/resources/application.properties 中的 application.properties,如下所示:
# Session properties
session.host=localhost
session.port=80
session.url=http://localhost:80/alfresco/api/-default-/cmis/versions/1.1/atom
session.compression=true
session.cache_ttl_objects=0
然后我尝试在使用它们的类中直接定义它们,但由于得到空值,我四处寻找为什么会发生这种情况并找到this。话虽如此,我遵循了应该可以解决此问题的 this 结构。
我创建了 SessionConfig.java 类:
package com.c4pa.cmisservice;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import io.quarkus.arc.config.ConfigProperties;
@ConfigProperties(prefix = "session") // Already tried without this, using session.url, etc on each Property
public class SessionConfig {
@Inject // Already tried without this
@ConfigProperty(name = "url")
private String url;
@Inject // Already tried without this
@ConfigProperty(name = "compression", defaultValue = "true")
private String compression;
@Inject // Already tried without this
@ConfigProperty(name = "cache_ttl_objects", defaultValue = "0")
private String cacheTTLObjects;
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
public String getCompression(){
return this.compression;
}
public void setCompression(String compression){
this.compression = compression;
}
public String getCacheTTLObjects(){
return this.cacheTTLObjects;
}
public void setCacheTTLObjects(String cacheTTLObjects){
this.cacheTTLObjects = cacheTTLObjects;
}
}
我正在尝试使用此类 CMISClient.java 的属性:
@ApplicationScoped
public class CMISClient {
private static Map<String, Session> connections = new ConcurrentHashMap<String, Session>();
public CMISClient() { }
@Inject
SessionConfig sessionConfig;
public Session getSession(String connectionName, String username, String pwd) {
Session session = connections.get(connectionName);
System.out.println(sessionConfig.getUrl() + "|" + sessionConfig.getCompression() + "|" + sessionConfig.getCacheTTLObjects());
if (session == null) {
// No connection to Alfresco available, creating a new one
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(SessionParameter.USER, username);
parameters.put(SessionParameter.PASSWORD, pwd);
parameters.put(SessionParameter.ATOMPUB_URL, sessionConfig.getUrl());
parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameters.put(SessionParameter.COMPRESSION, sessionConfig.getCompression());
parameters.put(SessionParameter.CACHE_TTL_OBJECTS, sessionConfig.getCacheTTLObjects());
...
}
return session;
}
...
}
但是当我调用调用 getSession 方法的端点时,它会在 getSession 方法的 println 上导致 org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException。当然,如果我对此发表评论,我会在 parameters.put 行上得到相同的异常。
如果有用,则反向堆栈跟踪:
java.lang.NullPointerException
at com.c4pa.cmisservice.CMISClient.getSession(CMISClient.java:46)
at com.c4pa.cmisservice.CMISResource.createConnection(CMISResource.java:42)
at com.c4pa.cmisservice.CMISResource.send(CMISResource.java:25)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:170)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:643)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:507)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:457)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:459)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:419)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:393)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:68)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:492)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)
at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:136)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:40)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:97)
at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:231)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Resulted in: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
... 20 more
关于为什么会发生这种情况的任何想法?提前非常感谢。
编辑:
@Path("/file")
public class CMISResource {
private CMISClient cmisClient;
private String connectionName;
private Session session;
@GET
@Path("/send")
@Produces(MediaType.TEXT_PLAIN)
public String send() throws IOException {
this.createConnection();
Folder folder = this.cmisClient.createFolder(session);
Document document = cmisClient.createDocument(session, folder);
return document.getName() + " was succesfully created (at least I hope so)";
}
private void createConnection() {
this.cmisClient = new CMISClient();
this.connectionName = "c4paAlf01";
this.session = cmisClient.getSession(connectionName, "username", "password");
}
【问题讨论】:
-
CMISClient是如何使用的? -
@geoand 我刚刚使用 CMISClient 使用类的代码编辑了问题
-
你确定他不会发生 NPE,因为
sessionConfig是 null 而不是调用sessionConfig方法? -
@LucaBassoRicci sessionConfig 为空。这就是问题所在(至少目前如此)。我从具有 ConfigProperty 的类中注入它,所以我不明白为什么它为空
标签: java properties config javabeans quarkus