如何解决为什么这些 application.properties 不起作用?
我一直在实现一个简单的 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");
}
解决方法
试试这个:
@ConfigProperties(prefix = "session")
public class SessionConfig {
public String url;
public String compression;
public String cacheTTLObjects;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。